length of vector

3 ビュー (過去 30 日間)
huda nawaf
huda nawaf 2011 年 11 月 5 日
hi, I want to know length of vector I have array as:
1 2 3 3 4 0 0 0 0
3 4 5 6 1 2 3 0 0
4 5 1 34 1 1 1 2 1
I need to know length of each row(non zero values) I know is 3*9, but i need to know length of each row with non zero values that found in end of row without using for loop or while
thanks
  1 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 11 月 5 日
After posting you question, if you care to take a look at your own question, you'll see something is not right (formating). You've been asking enough questions and seeing enough answers to learn to use the {}Code format.

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

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2011 年 11 月 5 日
d = reshape([1 2 3 3 4 0 0 0 0 3 4 5 6 1 2 3 0 0 4 5 1 34 1 1 1 2 1],9,[])'
out = sum(d~=0,2)

Walter Roberson
Walter Roberson 2011 年 11 月 5 日
sum(d(d(:,end)~=0,:) ~= 0,2)
Personally, though, I would have thought that if you want to know the number of non-zero elements in the rows that end in non-zero elements, that it would help to know which of the original rows each count was associated with. Or at least that a 0 be put in as a place-holder in the counts if the line ended in a 0 and thus was not to have its values counted.

カテゴリ

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