How to save the output of matrix calculation with different dimension

조회 수: 2 (최근 30일)
Moe
Moe 2016년 2월 22일
편집: Stephen23 2016년 2월 24일
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);
end
  댓글 수: 1
Stephen23
Stephen23 2016년 2월 24일
편집: Stephen23 2016년 2월 24일
@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:

댓글을 달려면 로그인하십시오.

채택된 답변

Roger Stafford
Roger Stafford 2016년 2월 23일
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:
http://www.mathworks.com/help/matlab/ref/max.html

추가 답변 (1개)

Guillaume
Guillaume 2016년 2월 22일
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);
end
Another way to obtain the same result:
index = cellfun(@(row) find(row == maxA), num2cell(A, 2), 'UniformOutput', false);
  댓글 수: 4
Moe
Moe 2016년 2월 23일
편집: Moe 2016년 2월 23일
Thanks Guillaume, I want to have seperate matrix for each cell in D. I used the following code:
D_set = cell2mat(D)
But, it stored all cell in one matrix, how can I have
D1 =
D2 =
D3 =
D4 =
.
.
.
Guillaume
Guillaume 2016년 2월 24일
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} =
...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by