While loop for the elements of an array
古いコメントを表示
I have an array:
a=[1 1 1 1 1 1 1 10 1 1 1 1 1 1 12 1 1 1 1 3];
I want to make a while loop that does the following
enas=0;
while a(i)==1 %
enas=enas+1;
end
But I don't know how to express it in matlab. Can you help me please?
1 件のコメント
Image Analyst
2013 年 5 月 27 日
It's recommended not to use i (the imaginary variable) as a variable name.
採用された回答
その他の回答 (1 件)
Jason Nicholson
2013 年 5 月 27 日
See the lines below. This will work.
a=[1 1 1 1 1 1 1 10 1 1 1 1 1 1 12 1 1 1 1 3];
i = 1;
enas=0;
while a(i)==1 %
enas=enas+1;
i = i +1;
end
4 件のコメント
Giorgos Papakonstantinou
2013 年 5 月 27 日
Giorgos Papakonstantinou
2013 年 5 月 27 日
Matt Kindig
2013 年 5 月 27 日
編集済み: Matt Kindig
2013 年 5 月 27 日
This should do it:
b = [0, a, 0]; %ensure that ends are not 1
edges = find(b~=1); %location elements that are not 1
spans = diff(edges)-1; %distance between edges is span of 1's
enas = spans(spans~=0) %should output 7 6 4
Giorgos Papakonstantinou
2013 年 5 月 28 日
カテゴリ
ヘルプ センター および File Exchange で Multidimensional Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!