How to save the output of matrix calculation with different dimension

5 views (last 30 days)
Moe on 22 Feb 2016
Edited: Stephen23 on 24 Feb 2016
Matrix A is as follows:
A = [0.535700142949073 0.979891844387851 0.971530253658353 0.0298279396616543 0.594892834326848 0.000439003167024231 0.000514749528323066 0.946720656164921 0.625164300616741 0.134751154634228
0.00152314772823977 8.49695682929950e-05 0.000136144135877524 0.000115671549970599 0.00489725988685105 0.998565384853671 0.998310619065266 0.000278522318895896 0.000660749829483171 0.000442148312929598
0.0778175636147425 0.00460710234090386 0.00710916350708422 0.00809442181570768 0.140394113265490 0.000557855340915817 0.000658209914611258 0.0147205564442349 0.0462953048979571 0.0292516708278132
0.384959145707945 0.0154160837029518 0.0212244386986851 0.961961966972667 0.259815792520811 0.000437756638388837 0.000516421491799660 0.0382802650719480 0.327879644655819 0.835555026225029];
I need a for loop for the following calculations and save them in separate matrices (because dimension of each index is different):
maxA = max(A);
index1 = find(A(1,:) == maxA);
index2 = find(A(2,:) == maxA);
index3 = find(A(3,:) == maxA);
index4 = find(A(4,:) == maxA);
% this is my code, but seems not correct!
n = 4;
for i=1:n;
index(i) = find(A(i,:) == maxA);
  1 Comment
Stephen23 on 24 Feb 2016
@Mohammad Hesam: Do not create numbered variables. Although beginners think that numbered variables are a great idea it inevitably leads them to ask how to dynamically access variable names. Bad idea! Just consider this: numbered variables are de facto indices, so therefor just turn them them into real indices, exactly like Guillaume has said several times already. Use a cell array (or numeric array if possible), exactly like Guillaume explained. This is a much faster and much more robust way to program.
Here is a long explanation of why accessing dynamic variables names is a really bad way to program:

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 23 Feb 2016
Based on the fact that you appear to be seeking four index values, I am guessing that what you actually want are the maximum values occurring in each of the four rows and their corresponding column indices. If so, this is what you should write:
[maxA,Index] = max(A,[],2);
The array 'maxA' will then be a four-element column vector containing the maximum values for each of the four rows. The array 'Index' will be a four-element column vector containing the (presumably) desired corresponding four column indices for these four maximums.
Read the description of 'max' in:

More Answers (1)

Guillaume on 22 Feb 2016
You do not want to save the result as separate numbered variables. As a rule, if you start numbering variables, you're doing it wrong.
In your case, you want to save your result as a cell array of vectors. The fix is easy just replace index(i) by index{i}, so:
for i=1:size(A, 1) %do not use hardcoded constants
index{i} = find(A(i,:) == maxA);
Another way to obtain the same result:
index = cellfun(@(row) find(row == maxA), num2cell(A, 2), 'UniformOutput', false);
Guillaume on 24 Feb 2016
I'll repeat what I said: "You do not want to save the result as separate numbered variables. As a rule, if you start numbering variables, you're doing it wrong."
Leave it as a cell array. It's no more difficult to address it as :
D{1} =
D{2} =
D{3} =

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by