Finding the row and column number in a matrix

for a matrix [1 2 3 4 6 10 7 5 9] mXn matrix-- how is it that i can find the min or max element and then find the row number and the column number for further use in calculations

1 件のコメント

Meryem
Meryem 2014 年 9 月 4 日
You can have an answer with a few lines of code which is:
%you have ndata matrix
[r,c] = size(ndata); %get row and column values of data matrix
fprintf('\nRow of data matrix is: %d' ,r); %print number of row
fprintf('\nColumn of data matrix is: %d ' ,c); %print number of column

サインインしてコメントする。

 採用された回答

Jan
Jan 2011 年 3 月 15 日

18 投票

data = rand(5, 3);
[maxNum, maxIndex] = max(data(:));
[row, col] = ind2sub(size(data), maxIndex);
Another less compact approach finds the max values for each column at first:
data = rand(5, 3);
[maxNumCol, maxIndexCol] = max(data);
[maxNum, col] = max(maxNumCol);
row = maxIndexCol(col);
Please read "help max" also.

13 件のコメント

Paulo Silva
Paulo Silva 2011 年 3 月 15 日
+1 vote
Matt Fig
Matt Fig 2011 年 3 月 15 日
This is the preferred method.
Raghuram
Raghuram 2011 年 3 月 17 日
thank you :)
Raghuram
Raghuram 2011 年 3 月 25 日
the method is not working for large sized matrices like i have to deal with a 30X600 matrix
Matt Fig
Matt Fig 2011 年 3 月 25 日
What do you mean "not working?" Are you saying that this approach gives the wrong result? Is there an error? If so, what error?
Jan
Jan 2011 年 3 月 25 日
@Raghuram: The method works properly for matrices of any size (tested with 40000x40000). If you get an error, it is caused by another problem. Please post the error message or explain the difference bewteen the results and your expectations.
Raghuram
Raghuram 2011 年 3 月 26 日
This was the code i used, F is a matrix that keeps increasing its size through columns that is 30X1 to 30 X 600
[minNumCol minIndexCol] = min(f(:));
[minNum col] = min(minNumCol);
row = minIndexCol(col);
the program is stopping at a point where the values are
row=32
col=1
minIndexCol=32
I have to find the least value and its row and column numbers
Raghuram
Raghuram 2011 年 3 月 26 日
?? Index exceeds matrix dimensions.
Error in ==> PSO1stage5double at 156
pggbest(i)=pg(row,col);
this is the error
Matt Fig
Matt Fig 2011 年 3 月 26 日
Raghuram, look carefully at the two different codes Jan gave you, then look at the code you are using. Your code is not the same as either method! You have made a hybrid of the two different methods, but that doesn't work as you found out.
Raghuram
Raghuram 2011 年 3 月 26 日
I used the first method only this time
the code was
[minNum1, minIndex1] = min(f(:));
[row1, col1] = ind2sub(size(f(:)), minIndex1);
error:
??? Attempted to access pg(32,1); index out of bounds because size(pg)=[30,2].
Error in ==> PSO1stage5double at 155
pggbest(i)=pg(row1,col1);
is there any mistake in the code, the notations i mean
Raghuram
Raghuram 2011 年 3 月 26 日
I used the first method only this time
the code was
[minNum1, minIndex1] = min(f(:));
[row1, col1] = ind2sub(size(f(:)), minIndex1);
error:
??? Attempted to access pg(32,1); index out of bounds because size(pg)=[30,2].
Error in ==> PSO1stage5double at 155
pggbest(i)=pg(row1,col1);
is there any mistake in the code, the notations i mean
Walter Roberson
Walter Roberson 2011 年 3 月 26 日
Don't use size(f(:)), use size(f)
Raghuram
Raghuram 2011 年 3 月 26 日
yea it works now, thank you :)

サインインしてコメントする。

その他の回答 (2 件)

Rajashree Jain
Rajashree Jain 2011 年 3 月 15 日

0 投票

[val row]=max(A(:));
[val col]=max(A(row,:));
[val row col];

1 件のコメント

Jan
Jan 2011 年 3 月 15 日
This will not work. In the first line you can get e.g. the last element of A as maximum, then "row==numel(A)". Then "A(row, :)" will fail.

サインインしてコメントする。

Amey
Amey 2011 年 3 月 15 日

0 投票

The first answer given by Jan Simon is absolutely right and the most efficient way to do it.

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

タグ

質問済み:

2011 年 3 月 15 日

コメント済み:

2014 年 9 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by