How to make three dimension matrix indexed sum return summation across the third dimension

1 回表示 (過去 30 日間)
I want to get a 2d matrix after summing 3d matrix based on an index. Or how can I project the indexed sum to a 2d matrix? The example below gives only the sum for the indexes.
datax = rand(3,3,5);
cr1 = int32(randi([0, 1], [1, 45]));
cr1 = reshape(cr1,3,3,5);
cr2 = int32(randi([0, 1], [1, 45]));
cr2 = reshape(cr2,3,3,5);
inddata = find(cr1==1 & cr2==0);
sum(datax(inddata),2)

採用された回答

Jan
Jan 2021 年 5 月 14 日
datax = rand(3,3,5);
cr1 = rand(3,3,5) > 0.5;
cr2 = rand(3,3,5) > 0.5;
ignore = (~cr1 | cr2);
result = sum(datax .* ignore, 3)
result = 3×3
1.2499 1.8071 1.0860 1.8841 1.6616 2.8713 2.8635 1.1758 2.0541
If you really need int32 data:
cr1 = randi([0, 1], size(datax), 'int32');
cr2 = randi([0, 1], size(datax), 'int32');

その他の回答 (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