A neat reliable way to add up section of a matrix

2 Ansichten (letzte 30 Tage)
maubars
maubars am 12 Dez. 2020
Kommentiert: maubars am 17 Dez. 2020
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

Akzeptierte Antwort

Rik
Rik am 12 Dez. 2020
Bearbeitet: Rik am 12 Dez. 2020
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)
1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30
% 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)
1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30 1 2 4 6 9 12 16 20 25 30
  8 Kommentare
Image Analyst
Image Analyst am 16 Dez. 2020
Bearbeitet: Image Analyst am 16 Dez. 2020
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.
maubars
maubars am 17 Dez. 2020
Wooow. Thank you very much, neat.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 12 Dez. 2020
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.
  1 Kommentar
maubars
maubars am 14 Dez. 2020
Thanks for the explanation. Indeed, direct coding without the cell function or loops seems more intuitive, however, I am adding or appending multiple matrices(various sizes) to big matrix. So it's quite cumbersome.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by