Matrix Element Transfer from one matrix to another

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)

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
Ken le 6 Oct 2016
Basically, the larger matrix say A plots the potentials. I have to select the lowest potential elements and put their address i.e. row#, column# in the nX2 matrix. Now the matrix A is random i.e. has no defined pattern. The selection of the lowest potentials in A is no problem but how to transfer the address (row, colomn) to the nX2 matrix ?
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
Ken le 6 Oct 2016
Thanks. To try another problem - how to find the steepest -ve gradient from A(1,1) to A(5,5)?

Connectez-vous pour commenter.

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
Ken le 6 Oct 2016
Thanks. Not sure how this will work. I have pasted my reply to the other answer below, this may clarify what I am after. Basically, the larger matrix say A plots the potentials. I have to select the lowest potential elements and put their address i.e. row#, column# in the nX2 matrix. Now the matrix A is random i.e. has no defined pattern. The selection of the lowest potentials in A is no problem but how to transfer the address (row, colomn) to the nX2 matrix ?
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
Ken le 6 Oct 2016
Thanks, very helpful. Now all I have to do is to find the steepest -ve gradient from say A(1,1) to A(9,8). Any help would be appreciated.
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.
I have to find the steepest path from start to destination, but can't do it directly as there are obstacles in the grid (denoted by 1) which I can't traverse. I tried the foll. but OptimalPath is not being printed and I get it wrong:
OptimalPath=zeros(20, 2);
iter=0; Index_OptimalPath=[3,7]; Min=SearchSolution(3,7);
while (Row < 8 & Col < 7)
while not(SearchSolution(Row,Col) == 1)
while not(SearchSolution(Row,Col) == 0)
iter = iter+1;
assert(maxIter>iter, 'maxIter assert triggered. Aborting.');
for X=1:1:3
for Y=1:1:3
if Min>SearchSolution(SearchStart(1+X),SearchStart(2+Y));
Min=SearchSolution(SearchStart(1+X),SearchStart(2+Y));
end
end
end
OptimalPath
end
end
Row=Row+1;
Col=Col+1;
end
Dijkstra's algorithm may be what you’re looking for. I’ve no experience with it, nor coding any optimal-path problems.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Question posée :

Ken
le 6 Oct 2016

Commenté :

le 6 Oct 2016

Community Treasure Hunt

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

Start Hunting!

Translated by