for loop to repeat specified number of times
1 view (last 30 days)
Show older comments
I have 40448x20 matrix. I need to extract 1st 9 rows in one loop iteration, next 9 rows in second iteration and so on.and store each iteration output to perform some calculation.
0 Comments
Accepted Answer
Alan Stevens
on 18 Sep 2020
If M is your 40448x20 matrix then something like
for i = 1:9:40448
K = M(i:i+8,:);
... other calculations with K
end
Since 40448 is not exactly divisible by 9 this will leave a few rows of M untouched at the end. You will have to deal with them separately.
5 Comments
Alan Stevens
on 18 Sep 2020
Oh yes! I guess you'll have to do the extra calculations within the loop! At the moment I'm not sure how to save the K's individually (though why would you want to, as you have all the numbers in the large matrix anyway?).
Alan Stevens
on 18 Sep 2020
Edited: Alan Stevens
on 18 Sep 2020
You could use a structure to store the smaller matrices:
c = 1;
for i = 1:9:40448
K(c).m = M(i:i+8,:);
c = c+1;
... other calculations with K
end
K is now a structure
Access by K(1).m, or K(2).m etc.
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!