Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select one element from each column of a matrix in matlab?

Tags:

matlab

a = [1 1 1; 2 2 2; 3 3 3];

b = [1 2 3];

How can I call one function to get a vector v[i] = a[b[i],i]?

like image 744
dalibocai Avatar asked Nov 18 '11 14:11

dalibocai


People also ask

How do you select an element from a matrix in MATLAB?

The most common way is to explicitly specify the indices of the elements. For example, to access a single element of a matrix, specify the row number followed by the column number of the element. e is the element in the 3,2 position (third row, second column) of A .

How do you select an element from a matrix?

Similar to vectors, you can use the square brackets [ ] to select one or multiple elements from a matrix. Whereas vectors have one dimension, matrices have two dimensions. You should therefore use a comma to separate the rows you want to select from the columns.

How do you split elements in a matrix in MATLAB?

x = A ./ B divides each element of A by the corresponding element of B . The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.


1 Answers

v = a(sub2ind(size(a), b, 1:length(b)))

sub2ind transforms subscripts into a single index.

like image 65
cyborg Avatar answered Oct 10 '22 21:10

cyborg