matrix addition according to position vector
2 次查看(过去 30 天)
显示 更早的评论
采纳的回答
Voss
2023-2-6
A=[1 3 5 7 9 11;
2 4 6 8 10 12];
b=[1 1 3 4 5 3];
Maybe something like this:
C = zeros(size(A));
ub = unique(b);
for ii = 1:numel(ub)
C(:,ub(ii)) = sum(A(:,b == ub(ii)),2);
end
disp(C);
Another way:
[bg,gidx] = findgroups(b);
C = zeros(size(A));
C(:,gidx) = splitapply(@(x)sum(x,2),A,bg);
disp(C);
0 个评论
更多回答(1 个)
Vilém Frynta
2023-2-6
This looks like homework.
It seems like you know what kind of functions you should work with. I'd recommend to study the documention of these functions and try to think it out. Feel free to share your progress.
If you are beginning with Matlab, it's good idea just to try (and learn from mistakes and experiments).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!