Matrix Element Transfer from one matrix to another

8 次查看(过去 30 天)
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.

回答(2 个)

James Tursa
James Tursa 2016-10-6
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 个评论
James Tursa
James Tursa 2016-10-6
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 2016-10-6
Thanks. To try another problem - how to find the steepest -ve gradient from A(1,1) to A(5,5)?

请先登录,再进行评论。


Star Strider
Star Strider 2016-10-6
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 个评论
Ken
Ken 2016-10-6
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
Star Strider
Star Strider 2016-10-6
Dijkstra's algorithm may be what you’re looking for. I’ve no experience with it, nor coding any optimal-path problems.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by