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

4 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2022-9-20
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.
Star Strider
Star Strider 2022-9-20
Or is there any specific way to use this condition on the cell directly?
Yes.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2022-9-20
Use the cellfun and nnz functions —
B = num2cell(rand(41));
A = nnz(cellfun(@(x)x<0.0038, B))
A = 5
.
  4 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by