How can I extract column vectors from matrix (with large number of columns) ?

조회 수: 63 (최근 30일)
Having a lot of columns, this way of extracting from a big matrix named C :
temperature = C(:,1);
index = C(:,2);
...
vu = C(:,35);
results, in this example, in 35 lines of matlab program ; is there a solution like
[temperature, index, ... , vu] = C;
Where left side is a "list" of (column) vectors ? If so, my program would be more compact and readable. Thank you.

채택된 답변

the cyclist
the cyclist 2012년 9월 20일
편집: the cyclist 2012년 10월 4일
In general, MATLAB would not be able to parse that syntax, because it could not predict that you want exactly one column per variable (as opposed to, say, two columns to temperature and one to index, etc.).
However, it is easy to write a function to do what you want:
function [varargout] = matrixToColumnVectors(X)
ncol = size(X,2);
for nc = 1:ncol
varargout(nc) = {X(:,nc)};
end
end
Usage example:
>> x = rand(7,3);
>> [x1,x2,x3] = matrixToColumnVectors(x)

추가 답변 (1개)

Honglei Chen
Honglei Chen 2012년 9월 20일
Here is a simple example. Not sure if it's exactly what you want, but it may get you started
x = magic(3)
y = mat2cell(x,size(x,1),ones(1,size(x,2)))
[a,b,c] = deal(y{:})

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by