How to count the element of a cell?
1 次查看(过去 30 天)
显示 更早的评论
hello all,,
i wanna ask u about how to count the element of a cell,,
i mean like this
example :
[1] [4] [13] 'low'
[7] [7] [16] 'high'
[7] [4] [25] 'high'
in the picture above we can see any two data that have category 'high' and one data has category 'low'
i've trouble how to count that category so the result like this
high : 2
low : 1
in my case, i want if amount of data that have category 'high' is more than amount of data that have category 'low' so the result is "new data category is high"
can anyone help me?
please :)
sorry for my bad english :)
0 个评论
采纳的回答
Matt J
2013-1-21
编辑:Matt J
2013-1-21
categories=CellArray(:,4);
if nnz(ismember(categories,'high')) > nnz(ismember(categories,'low'))
disp 'new data category is high'
end
7 个评论
Matt J
2013-1-22
In past MATLAB versions, I used to find that the speed difference was a function of the number of zeros vs. ones. I don't see that anymore. However, for sparse matrices, it seems pretty clear that NNZ is the thing to use
>> A=sprand(1e6,1e3,.001); tic; nnz(A); toc; tic;sum(A(:));toc
Elapsed time is 0.000139 seconds.
Elapsed time is 0.008363 seconds.
Cedric
2013-1-22
编辑:Cedric
2013-1-22
Interestingly, the gap reduces when we set them up to perform what could be the same kind of operations:
>> n=1e4;tic;for ii=1:n,nnz(A);end;toc
Elapsed time is 0.017255 seconds.
>> n=1e4;tic;for ii=1:n,sum(A(:));end;toc
Elapsed time is 85.381213 seconds.
>> n=1e4;tic;for ii=1:n,sum(sum(A));end;toc
Elapsed time is 10.520439 seconds.
>> B = logical(A) ;
>> n=1e4;tic;for ii=1:n,nnz(B);end;toc
Elapsed time is 0.017807 seconds.
>> n=1e4;tic;for ii=1:n,sum(sum(B));end;toc
Elapsed time is 0.137510 seconds.
EDIT: Ratio <8 between the two latter, over 1e4 iterations, means that the relative difference is not that big.
I find this quite strange actually, because I thought that nnz was stored in the data structure of sparse matrices, and that nnz() was just returning this number for them.
[Gilbert at al., Golub 60th birthday] ... " These goals are met by a simple column-oriented scheme that has been widely used in sparse matrix computation. A sparse matrix is a C record structure with the following constituents. The nonzero elements are stored in a one-dimensional array of double-precision reals, in column major order. (If the matrix is complex, the imaginary parts are stored in another such array.) A second array of integers stores the row indices. A third array of n + 1 integers stores the index into the rst two arrays of the leading entry in each of the n columns, and a terminating index whose value is nnz. Thus a real mn sparse matrix with nnz nonzeros uses nnz reals and nnz + n + 1 integers. "
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!