How to apply a relational operator on a cell in MATLAB?

I would like to know the number of values that are less than 0.0038 in a cell of 41 X 41 double.
For example, I have the cell below:
B = num2cell(rand(41));
When I applied such condition
sum(A(:) < 0.0038)
, it gave me an error, which is Operator '<' is not supported for operands of type 'cell'.
So, do I need to convert the cell to a matrix and apply the operation? Or is there any specific way to use this condition on the cell directly?

2 Comments

B = num2cell(rand(41));
Why use such an inefficient approach to storing numeric data?
Most likely your task would be much simpler if you used a numeric array.
Or is there any specific way to use this condition on the cell directly?
Yes.

Sign in to comment.

 Accepted Answer

Use the cellfun and nnz functions —
B = num2cell(rand(41));
A = nnz(cellfun(@(x)x<0.0038, B))
A = 5
.

4 Comments

@Star Strider I really appreciate your answer, but it did not work. It asked me first to change 'UniformOutput' to false and when I changed it, I faced this error (Incorrect number or types of inputs or outputs for function 'nnz'.) because my size data type is double.
"It asked me first to change 'UniformOutput' to false "
Your actual cell content are non-scalar, unlike the example data you gave in your question. An inaccurate data description in your question delays you getting a useful answer that works on your actual data.
What do you expect SUM to return for a set of arrays (potentially of different sizes) ?
@Stephen23 Thank you for your notice. I got it now. Thank you again.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!