How can I use Loop to read multiple matrixes and sum rows?

Hello there.
I have the A.mat file that contains 28 different matrices (A1,A2...A28) each one with 100 rows x 100 columns. I would like to sum rows of these matrices to get 28 row-vector, using loop.
Any clue?

 Accepted Answer

You can load the mat file to a struct and loop through the fields. Now you also immediately see why numbered variables are a bad idea.
S=load('A.mat');
A=zeros(100,100,28);
for n=1:28
fn=sprintf('A%d',n);
A(:,:,n)=S.(fn);
end
Now you have a 3D array where you can trivially sum:
B=sum(A,3);

2 Comments

You're welcome.
If my answer solved your issue, please consider marking it as accepted answer. If not, feel free to comment with your remaining questions.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 28 Feb 2022

Commented:

Rik
on 28 Feb 2022

Community Treasure Hunt

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

Start Hunting!