could anyone help me how to display the position of all the numbers present in matrix.
1 view (last 30 days)
Show older comments
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 Comments
Stephen23
on 11 Sep 2019
>> 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.
Answers (2)
Fabio Freschi
on 11 Sep 2019
Edited: Fabio Freschi
on 11 Sep 2019
[iRow, jCol, value] = find(A);
then you can put them in a matrix, if you like
position = [value, iRow, jCol];
1 Comment
Stephen23
on 11 Sep 2019
Note that this method will ignore zeros. Last time I checked, zero was still a number.
Stephen23
on 11 Sep 2019
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
0 Comments
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!