Matrix Element Transfer from one matrix to another
Afficher commentaires plus anciens
I have a matrix of 10X10. I want to select elements of this matrix and put them in an N X 2 Matrix. As example, if one of the selected elements of the 10X10 matrix is at 10, 12 I want this 10,12 to be transferred to the N X 2 matrix. Hence, basically the question I am asking is how to transfer position i.e row,col of a matrix from one to the other.
Réponses (2)
James Tursa
le 6 Oct 2016
I am not quite clear what you are asking. Is it something like this?
>> A = reshape(1:100,10,10) % <-- test data
A =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
>> [row,col] = find(A==47) % <-- pick off row & column of location of 47
row =
7
col =
5
>> B(1,1:2) = [row,col] % Stuff the row & col into the 1st row of B
B =
7 5
>> [row,col] = find(A==59) % <-- pick off row & column of location of 59
row =
9
col =
6
>> B(2,1:2) = [row,col] % Stuff the row & col into the 2nd row of B
B =
7 5
9 6
3 commentaires
Ken
le 6 Oct 2016
James Tursa
le 6 Oct 2016
Does this do what you want?
>> A = reshape(randperm(25),5,5)
A =
17 2 24 14 8
1 23 16 10 4
11 12 20 18 15
6 9 22 13 5
25 3 21 19 7
>> [row,col] = find(A<10); % <-- find where A is less than 10
>> result = [row col]
result =
2 1
4 1
1 2
4 2
5 2
1 5
2 5
4 5
5 5
Ken
le 6 Oct 2016
Star Strider
le 6 Oct 2016
If I understand what you want, the find function will do exactly that.
Example:
M = randi(9, 10); % Create Matrix
[row,col] = find(M == 3); % Return Rows & Cols Of All ‘3’ Values
6 commentaires
Ken
le 6 Oct 2016
Star Strider
le 6 Oct 2016
To find the coordinates of all the minimum values of ‘A’, the code changes slightly:
A = randi(9, 10); % Create Matrix
[row,col] = find(A == min(A(:))); % Return Rows & Cols Of All ‘min(A)’ Values
Coordinates = [row,col]; % Coordinates Matrix
Ken
le 6 Oct 2016
Star Strider
le 6 Oct 2016
My pleasure.
I have no idea what path you want to take from ‘A(1,1)’ to ‘A(9,8)’. Use the gradient function on your matrix, then define that path and choose the minimum. You may want to use interp2, griddedInterpolant, or a similar interpolation scheme to refine the grid of your (10x10) matrix to provide a more direct path.
The path from ‘A(1,1)’ to ‘A(5,5)’ is a simple diagonal, so first use the diag and then the gradient functions, then simply find the minimum (use the min function) of the gradient of the diagonal between those matrix elements.
Ken
le 6 Oct 2016
Star Strider
le 6 Oct 2016
Dijkstra's algorithm may be what you’re looking for. I’ve no experience with it, nor coding any optimal-path problems.
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!