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)
0 个评论
采纳的回答
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)
If you really need int32 data:
cr1 = randi([0, 1], size(datax), 'int32');
cr2 = randi([0, 1], size(datax), 'int32');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!