0

I am a beginner in Matlab and have not been able to find an answer to my question so far. Your help will definitely be very much appreciated.

I have 70 matrices (100x100), named SUBJ_1, SUBJ_2 etc. I would like to create a loop so that I would calculate some metrics (i.e. max and min values) for each matrix, and save the output in a 70x2 result matrix (where each row would correspond to the consecutively named SUBJ_ matrix).

I am struggling with both stages - how to use the names of individual variables in a 'for' loop and how to properly save individual outputs in a combined array.

Many thanks and all the best!

1
  • See related answer. The eval(sprintf(...)) is the code/workaround/hack you want, but please note the warning in the beginning (similar to what Daniel and Jonas mentioned). Commented Sep 1, 2014 at 9:06

3 Answers 3

4

Don't use such variable names, create a big cell array named SUBJ and put each Matrix in it.

r=zeros(numel(SUBJ),2)
for idx=1:numel(SUBJ)
   r(idx,1)=min(min(SUBJ{idx}))
   r(idx,2)=max(max(SUBJ{idx}))
end

min and max are called twice because first call creates maximum among rows, second call among columns.

Sign up to request clarification or add additional context in comments.

1 Comment

A cell array containing the matrices also works, but I would expect it to be slower than the 3d matrix I give in my solution. It has the advantage though that all 70 matrices do not have to be the same size.
3

Even though this is in principle possible in Matlab, I would not recommend it: too slow and cumbersome to implement.

You could instead use a 3-D matrix (100x100x70) SUBJ which would contain all the SUBJ_1 etc. in one matrix. This would allow you to calculate min/max etc. with just one line of code. Matlab will take care of the loops internally:

OUTPUT(:,1) = min(min(SUBJ,[],1)[],2);
OUTPUT(:,2) = max(max(SUBJ,[],1)[],2);

Like this, OUTPUT(1,1) contains min(min(SUBJ(:,:,1))) and so on...

4 Comments

A big matrix is even better than a cell array, but your code is not correct. ALLMINS = min(min(SUBJ,[],1)[],2); would produce the correct result.
@Daniel, thanks for that - didn't have MATLAB open and too long since I used it much :-/
Hi both! Many thanks for your suggestions. While this is practical for this particular (first) task, for a later stage of my data processing, I would still need to somehow create a loop through arrays corresponding to individual subjects. I will need to run sone previously prepared scripts for which the arrays would need to be kept at their original sizes. Would it therefore still be somehow possible to create a loop through individual arrays based on subject names? Thanks!
To loop through individual subjects you simply loop over the index ii in SUBJ(:,:,ii). SUBJ(:,:,1) is identical to SUBJ_1(:,:); the only difference is notation. The advantage is that it is trivial to increment the index for SUBJ(:,:,1) in Matlab, while incrementing the variable name for SUBJ1(:,:) is difficult and ugly.
1

As to how to use the names of individual variables in a 'for' loop, here gives an example:

SUBJ = [];
for idx = 1:70
    term = eval(['SUBJ_',num2str(idx)]);
    SUBJ = [SUBJ; max(max(term)),min(min(term))];
end

3 Comments

This is what @rochned actually asked for, but as mentioned in the other comments it is not really recommended, e.g. for speed reasons. Whenever you are tempted to use eval, there is probably a more efficient way to do things...
Yeah, I admit that, especially when you have massive data to deal with.
Thanks, @Highman - this is exactly what I needed! For the current dataset (and other scripts), this seems to work really well. Many thanks also to everyone else for your advice and explanations!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.