Let's explained with an example if I have h =
0 1 2 3
1 0 0 0
5 0 1 0
there is any function that will return the number of nonzero elements per row something like c =
3
1
2
but without a loop. I know I can use nnz per row
something like
for i=1: numRows
c(i)=nnz(h(i,:));
end
but there is any way to do it without a loop?
I will really appreciate any suggestions

 採用された回答

Walter Roberson
Walter Roberson 2011 年 2 月 8 日

5 投票

c = sum(h~=0,2);

6 件のコメント

Maider Marin
Maider Marin 2011 年 2 月 8 日
no, maybe I did not explain well, I want to count how many nonzero elements are in each matrix row and have in return a vector of the same size of the matrix's row and in the ith vector's entry the number of nonzero entries in the ith matrix's row
Matt Fig
Matt Fig 2011 年 2 月 8 日
That is funny. Did you even run Walter's code?
Walter Roberson
Walter Roberson 2011 年 2 月 8 日
Maider, your example clearly shows a column vector being returned, which is what my code does.
If c is not initialized, then a loop
for i=1:numRows
c(i) = something
end
will create a _row_ vector rather than a column vector. If that is the shape of the result you want, [3 1 2] instead of [3;1;2], then just add .' before the semi-colon in my code:
c = sum(h~=0,2).';
My original code computes *exactly* what you asked for in your example.
Matt Fig
Matt Fig 2011 年 2 月 14 日
Maider, AGAIN -- Did you try Walter's code??
It gives what you are asking, why do you not want to get there using a specific function??
Maider Marin
Maider Marin 2011 年 2 月 14 日
yes, it is true...my familiarity with the command did not let me, understand it. Thans a buch Walter and Matt
Paulo Silva
Paulo Silva 2011 年 3 月 15 日
B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N, where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.

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

その他の回答 (3 件)

stef stef
stef stef 2011 年 3 月 15 日

0 投票

thank you Walter!Your answer worked fine with me, although i didn't exactly understand what 0,2 does..I thought sum was only to add values of elements.

1 件のコメント

Mbvalentin
Mbvalentin 2016 年 3 月 14 日
It's not a decimal point (like a = 0,2). The comma is just dividing the two input arguments that the 'sum' function can take. In this case he is first creating a logical matrix that has ones for every element in h that is not equal to 0 (that's what h ~= 0 does), and then this result vector is inputed in the sum function.
Now, the sum function does the summatory of the input vector (or matrix) in a certain direction. The default direction is '1' (this is, along the row direction). I.E., assume we have the following matrix:
M = [10 10 0; 0 10 1; 1 0 1];
The result of L = (M ~= 0) would be:
L = [1 1 0; 0 1 1; 1 0 1];
Now, the results of the sum of 'M' on each direction are:
sum(M,1) = [11, 20 2]; sum(M,2) = [20; 11; 2].
As the result of the logical matrix L is a one row vector, we need to add the values along the 'columns-direction', which is '2'. That's why here Walter used the sum(MATRIX,2), to sum along the columns.

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

Paulo Silva
Paulo Silva 2011 年 3 月 15 日

0 投票

Another option but not so good like Walter suggestion
a=[0 1 2 3
1 0 0 0
5 0 1 0]
sum(arrayfun(@any,a(1:size(a,1),:)),2)
ans =[3;1;2]
Gabriel
Gabriel 2013 年 7 月 2 日

0 投票

Keep in mind, the previous answers may work, but they require a lot of memory if your array is big (basically duplicates it).
If working with a LOT of data and facing out of memory errors, the for loop with nnz might be the way to go.

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by