Looping through Different Variables
1 view (last 30 days)
Show older comments
I am trying to loop through different matrices. For example I want to do something like this:
for i = [ Variable Constant Dataset Slopes ...]; ( where these are all arrays )
averageNumber = average(i(2,:));
matrix = [matrix averageNumber] (matrix is initialized before loop)
end
I know this does not work, but its the best way to portray my problem.
Any help would be appreciated!
edit: The variables are not necessarily the same size. They are 2-D arrays however. Basically I want to preform the same list of operations on different variables.
0 Comments
Answers (1)
Matt J
on 2 Jun 2014
Edited: Matt J
on 2 Jun 2014
I don't fully understand your pseudocode. If the matrices you're trying to loop through are 2D arrays of the same size MXN, then they should all be made slices of an MxNxP three-dimensional array A. Then, you would do something like this,
for i=1:P
currentMatrix=A(:,:,i);
%do other stuff
end
3 Comments
Stephen23
on 4 Mar 2020
Edited: Stephen23
on 4 Mar 2020
"matlab doesnt allow for brace indexing"
In fact it does. Both cell arrays (as Matt J recommended in this comment) and tables use curly braces to access data arrays within the container array. This is clearly explained in the documentation:
See Also
Categories
Find more on Loops and Conditional Statements 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!