A neat reliable way to add up section of a matrix
2 次查看(过去 30 天)
显示 更早的评论
I am looking for a neat way to sum up the below matrix without using loops. Note that the A to E have dirrent sizes. I have tried to use accumarray function, however it is not intuitive. I have referenced the last position and counted backwards to the first position based on the size of the matrix. However, my question is about the best way to uploading matrices to a master array with some sort of flexibility on positions. Additionally I would like to know about an effecient way to upload several matrices, various sizes to a master array, maybe using array functions or cell arrays?
A = repmat(1:10,10,1)
B = repmat(1:8,10,1)
C = repmat(1:6,10,1)
D = repmat(1:4,10,1)
E = repmat(1:2,10,1)
Z=zeros(size(A)) + A
Z(:, 3:10) = Z(:, 3:10) + B
Z(:, 5:10) = Z(:, 5:10) + C
Z(:, 7:10) = Z(:, 7:10) + D
Z(:, 9:10) = Z(:, 9:10) + E
0 个评论
采纳的回答
Rik
2020-12-12
编辑:Rik
2020-12-12
Loops tend to do the trick:
A = repmat(1:10,10,1);
B = repmat(1:8,10,1);
C = repmat(1:6,10,1);
D = repmat(1:4,10,1);
E = repmat(1:2,10,1);
data={A,B,C,D,E}; % put the data in 1 cell array
sz=[1 1];%pre-allocate the sz array
% cellfun('ndims',data) is faster than cellfun(@ndims,data), but there are only some functions supported
for n=1:max(cellfun('ndims',data)) % loop through all dimensions (taking the largest)
sz(n)=max(cellfun('size',data,n));% store the maximum size of each dimension
end
%If sz is not a two-element vector at this point, the next loop should be edited.
Z=zeros(sz);
for n=1:numel(data)
%find the indices to the last n columns
ind=sz(2) + (1-size(data{n},2)):0;
Z(:,ind)=Z(:,ind)+data{n};
end
disp(Z)
% confirming this is the same as your code:
A = repmat(1:10,10,1);
B = repmat(1:8,10,1);
C = repmat(1:6,10,1);
D = repmat(1:4,10,1);
E = repmat(1:2,10,1);
Z=zeros(size(A)) + A;
Z(:, 3:10) = Z(:, 3:10) + B;
Z(:, 5:10) = Z(:, 5:10) + C;
Z(:, 7:10) = Z(:, 7:10) + D;
Z(:, 9:10) = Z(:, 9:10) + E;
disp(Z)
8 个评论
Image Analyst
2020-12-16
编辑:Image Analyst
2020-12-16
For example:
Z = rand(4, 5); % Sample data
[rows, columns] = size(Z)
% Get a list of the indexes of the rightmost column
% in [row, column] format.
lastColumnIndexes = [(1:rows)', columns * ones(rows, 1)]
% Get a list of the indexes of the bottom row.
% in [row, column] format.
lastRowIndexes = [rows * ones(columns, 1), (1:columns)']
You'll see
lastColumnIndexes =
1 5
2 5
3 5
4 5
lastRowIndexes =
4 1
4 2
4 3
4 4
4 5
Note the first column is the row index and the second column is the column index of the elements.
更多回答(1 个)
Image Analyst
2020-12-12
Not sure the looping method is any better, faster, simpler, or more intuitive. Clearly it isn't because you're asking how it works. Your original code was fine (for a low number of arrays like 5, though it could be simplified by getting rid of zeros().
Z = A % No zeros needed
Z(:, 3:10) = Z(:, 3:10) + B;
Z(:, 5:10) = Z(:, 5:10) + C;
Z(:, 7:10) = Z(:, 7:10) + D;
Z(:, 9:10) = Z(:, 9:10) + E
If you have only a small number of matrices, like 5 or so, then sometimes it's better to keep in simple and intuitive rather than try to do it in loops. Even if loops make the code shorter by 2 or 3 lines, the decrease in readability might make it not worth doing and better to keep it simple and readable. So, for this case, I disagree with you when you say "it is not intuitive". Now, if you had dozens of matrices, then you would want to do a looping way like Rik showed you, or better yet, just start with a properly indexed array to start with so you don't need to build it from separate variables.
另请参阅
类别
在 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!