put the result in one matrix

2 ビュー (過去 30 日間)
yousef Yousef
yousef Yousef 2014 年 4 月 13 日
コメント済み: Star Strider 2014 年 4 月 14 日
Hi,I have ,w=[2 4 4 1 1].
for i=1:length(w)
x=find(w==w(i))
end
this code gives this result:x=[1]',x=[2 3]',x=[4 5]',x=[4 5]',in each iteration.
I want the result to be x=1 0 0 0
2 3 0 0
4 5 0 0
4 5 0 0

採用された回答

Star Strider
Star Strider 2014 年 4 月 13 日
Actually, your code does not give the result you post when I run it. The second row, [2 3 0 0] is repeated (as it should be) in the third.
I suggest:
w=[2 4 4 1 1];
x = zeros(4,4);
for i=1:length(w)
wi = find(w==w(i))
x(i,1:length(wi))=wi;
end
that produces:
x =
1 0 0 0
2 3 0 0
2 3 0 0
4 5 0 0
4 5 0 0
  4 件のコメント
Le Huy
Le Huy 2014 年 4 月 14 日
編集済み: Le Huy 2014 年 4 月 14 日
hello everyone! excuse me! could you please explain the code "x(i,1:length(wi))=wi" to me? i want to know what does the figure "1:length(wi)" mean? thank you!
Star Strider
Star Strider 2014 年 4 月 14 日
LeHuy — The ‘1:length(wi)’ statement creates a vector starting at 1 with spacing of 1 to whatever the length of wi is for that loop. If wi=3, the vector is [1 2 3]. Please see the documentation on the colon ‘:’ operator for more details.

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

その他の回答 (0 件)

カテゴリ

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