Looping through Different Variables

1 view (last 30 days)
stebin philip
stebin philip on 2 Jun 2014
Edited: Stephen23 on 4 Mar 2020
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.

Answers (1)

Matt J
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
Denzil Afriyie
Denzil Afriyie on 4 Mar 2020
matlab doesnt allow for brace indexing
Stephen23
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:

Sign in to comment.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!