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