could anyone help me how to display the position of all the numbers present in matrix.

조회 수: 1 (최근 30일)
I am having a matrix A=[1 2 3 4;
5 6 7 8;
9 10 11 12]
could anyone help me how to display the position of all the numbers in matrix.
  댓글 수: 3
Stephen23
Stephen23 2019년 9월 11일
>> fprintf('val: %3d pos: %3d\n',[A(:).';1:numel(A)])
val: 1 pos: 1
val: 5 pos: 2
val: 9 pos: 3
val: 2 pos: 4
val: 6 pos: 5
val: 10 pos: 6
val: 3 pos: 7
val: 7 pos: 8
val: 11 pos: 9
val: 4 pos: 10
val: 8 pos: 11
val: 12 pos: 12
If that is not what you want, then you need to explain your question better.
jaah navi
jaah navi 2019년 9월 11일
I want to have the result in the following order
value row column
1 1 1
5 2 1
9 3 1
2 1 2
6 2 2
10 3 2
3 1 3
7 2 3
11 3 3
4 1 4
8 2 4
12 3 4

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

답변 (2개)

Fabio Freschi
Fabio Freschi 2019년 9월 11일
편집: Fabio Freschi 2019년 9월 11일
[iRow, jCol, value] = find(A);
then you can put them in a matrix, if you like
position = [value, iRow, jCol];

Stephen23
Stephen23 2019년 9월 11일
A simple method that includes all numbers (because zeros are also numbers):
>> A = [1,2,3,0;5,6,0,8;9,10,11,12]
A =
1 2 3 0
5 6 0 8
9 10 11 12
>> S = size(A);
>> [R,C] = ndgrid(1:S(1),1:S(2));
>> [A(:),R(:),C(:)]
ans =
1 1 1
5 2 1
9 3 1
2 1 2
6 2 2
10 3 2
3 1 3
0 2 3
11 3 3
0 1 4
8 2 4
12 3 4

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by