필터 지우기
필터 지우기

How to count the element of a cell?

조회 수: 1 (최근 30일)
baby
baby 2013년 1월 21일
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 :)

채택된 답변

Matt J
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
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
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개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by