How can I divide a matrix in to vectors?

1 回表示 (過去 30 日間)
Ami
Ami 2011 年 6 月 6 日
C = [0 0 0 0 1; 1 0 0 0 0; 0 0 0 1 0; 0 0 1 0 0; 0 1 0 0 0;]
need something like this
c1=[0 0 0 0 1]
c2=[1 0 0 0 0]
I try to do it like this, but it doesn't work
for j=1:c for i=0:5 vector=1:5; end end

回答 (3 件)

Matt Fig
Matt Fig 2011 年 6 月 6 日
Please read this link before you even think about creating many vectors from an array like this:

phani
phani 2011 年 6 月 6 日
hi ami try this c1=C(1,:); c2=C(2,:);
i hope it will work.

Andrei Bobrov
Andrei Bobrov 2011 年 6 月 6 日
variant 1
Ccell = mat2cell(C,ones(size(C,1),1),size(C,2));
[C1,C2,C3,C4,C5] = Ccell{:};
variant 2
for jj = 1:size(C,1)
js = num2str(jj);
eval(['C', js , '= C(',js,',:)'] );
end
  1 件のコメント
Jan
Jan 2011 年 6 月 6 日
I really think, that the first line "Ccell = mat2cell(C,ones(size(C,1),1),size(C,2))" is enough already. Then the searched variables are Ccell{1}, Ccell{2}, etc. This would be more efficient than creating C1, C2, ... dynamically.
Although your answer matches the question exactly, I'm prefering Matt's "there are better ways" answer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by