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

1 visualizzazione (ultimi 30 giorni)
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)

Risposta accettata

Jan
Jan il 14 Mag 2021
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');

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by