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 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