How to deal with the presence of [] during the summation of arrays

1 回表示 (過去 30 日間)
Chao Zhang
Chao Zhang 2021 年 11 月 13 日
回答済み: Srivardhan Gadila 2021 年 11 月 16 日
Hi all,
There are four columns in each row, I want to add four arrays in each row to get a new array, but some of the rows have [], and I get an error when I run the addition, how can I handle it? The screenshot of structure is presented following:
I wrote some code, but it was wrong and did not work
for m = 1 : 5
if ~isempty(O(m).A1) || ~isempty(O(m).A2) || ~isempty(O(m).A3) || ~isempty(O(m).A4)
O(m).A5 = O(m).A1 + O(m).A2 + O(m).A3 + O(m).A4;
end
end
Thanks in advance for the support and help!

採用された回答

Srivardhan Gadila
Srivardhan Gadila 2021 年 11 月 16 日
One possible way is as follows:
a = [];
b = [];
c = randn(10,1);
d = randn(10,1);
sumResult = sum(cat(2,a,b,c,d),2);
Refer to the documentation of functions sum & cat for more information.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by