Find sum of elements in a cell along the columns

13 ビュー (過去 30 日間)
Vishakha Ramani
Vishakha Ramani 2019 年 9 月 9 日
回答済み: Jacob Shulman 2019 年 9 月 9 日
Hi all,
I have a 3D cell A with dimensions 3x5x2. An example of elements along row 2, all columns and z dimension = 1 is:
x = A(2,:,1)
x = [ ] [ ] 5.1 [ ] 7.9
I would like to add up the non zero entries and get the number of non zero entries in this resulting array. I tried using:
y = sum([A(2,:,1)])
and
y = cumsum([A(2,:,1)])
But got the following error:
"Error using sum
Invalid data type. First argument must be numeric or logical."
Could anyone please help me with this and would kindly let me know the procedure to solve such problems.
  1 件のコメント
Fabio Freschi
Fabio Freschi 2019 年 9 月 9 日
Could you please load your data in a mat file?

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

採用された回答

James Tursa
James Tursa 2019 年 9 月 9 日
編集済み: James Tursa 2019 年 9 月 9 日
Is this what you are trying to do? (using the curly braces)
y = sum([A{2,:,1}])
  1 件のコメント
Vishakha Ramani
Vishakha Ramani 2019 年 9 月 9 日
Thank you. Can't believe this was that simple. Also, could you please tell me how would you find the number of non-zero entries in such a case?

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

その他の回答 (1 件)

Jacob Shulman
Jacob Shulman 2019 年 9 月 9 日
A=cell(2,2,2)
A{2,1,1}=1;
A{2,2,1}=2;
B=0;
[rows,columns,depth]=size(A);
for i=1:rows
for j=1:columns
if nonzeros(A{i,j,1})
B=B+1;
end
end
end
y = sum([A{2,:,1}])
B
The function nonzeros will not count zeros or blanks.

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by