how to create different column as a different vector from a matrix

Hi, I am a new user of matlab. I am having a huge matrix (m,n)and I want to convert n different column vectors. How to do that? for example I am trying to do in following way
a=[1 2 3;4 5 6;7 8 9;10 11 12];
for i = 1 : size(a, 2)
qj = a(:,i)
end
but it is only storing last column . But I want to store like q1= 1 2 3 q2= 4 5 6
and so on please help me this regard. Thanks in advance.
Abhi

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 13일
Don't use q1, q2, q3. Use the following. You can reference it using q{1},q{2},q{3}. Even further, q{1}(2), q{3}(4).
a=[1 2 3;4 5 6;7 8 9;10 11 12];
q=cell(size(a,2),1);
for i = 1 : size(a, 2)
q{i} = a(:,i);
end

댓글 수: 8

I have tried that as well but it is showing error. see the error:
"??? Cell contents assignment to a non-cell array object.
Error in ==> seperation at 3
q{i} = a(:,i);"
You probably have q pre-defined. Do clear q first or better define q as an empty cell array first.
q=cell(size(a,2),1)
The error is caused by something else. If you run clear q first and then run the above code. There shouldn't be any error.
@Fangjun: Please insert the "q = cell(size(a, 2), 1)" code in your answer. Then I'll vote for it immediately.
Hah, Jan! It looks like I've earned a vote. See my first comment. I'll also put it in the answer.
you ppl were right. q was another variable in the code. Now it is working fine. I have just started using matlab can you guys suggest some reading material specifically for data processing and curve manupulation.thanks for your help
+1. The term "immediately" meant "as soon as I read it again".
Go through the document, under MATLAB->Data Analysis, Graphics

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 9월 13일

0 개 추천

Please see this FAQ

댓글 수: 3

Hi Walter.....sorry I could not understand it . Is it that we can not save or copy the separate column.As I am a very new user please can u help me to understand it .
Thanks and Regards
Abhinav
@Abhinav: The FAQ tells, that it is much more efficient to use the columns of a large array, that to split the array into vectors. Using variables called q1, q2, q3, ... will lead to problems and makes the code slow, hard to expand (if you need more vectors, you have to edit all occurences) and much harder to debug. Therefore it is recommended to use q(:,i) directly instead.
Please take into account, that this topic is included in the FAQ, because it leads to problems very frequently.
I appriciate ur help Thanks

댓글을 달려면 로그인하십시오.

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by