Reputation: 201
Question
If there is a matrix of MxN, how can extract all of the data based in the columns?
Im interesting to pass each column to a function and to be plotted.
if A(:) is used all the matrix is merged into a single column, (I remember this command is intended for that) but this does not serve to me.
Upvotes: 0
Views: 459
Reputation: 672
Matlab arrays use the indexing, partOfArray = array(rows, columns)
. The variables rows and columns can be a vector (including a scalar, which is a vector of length 1) or :
which is effectively interpreted by Matlab as meaning 'all' (e.g. array(:,columns)
would be all rows of the selected columns).
Alternatively, Matlab also allows linear indexing, in which array(aNumber)
counts in order from array(1,1)
first by rows, then by columns. e.g. if array is 2x4, array(5)
is equivalent to array(2,1)
. When you call A(:)
Matlab interprets that as using linear indexing to access all elements in the array, hence merging the matrix into a single column.
To access each column vector in a for loop, in this case printing it out, use:
A = magic(4)
numColumnsInA = size(A,2);
for i=1:numColumnsInA
disp(A(:,i))
end
You can find more information about indexing in Matlab here: Array Indexing
Upvotes: 2