select a band of a matrix
1 次查看(过去 30 天)
显示 更早的评论
Hello I want to select a band of a matrix with slop (S) and save them in another matrix.
a =
[ 1 2 3 4 5 6 7 8 9;
11 12 13 14 15 16 17 18 19;
21 22 23 24 25 26 27 28 29;
31 32 33 34 35 36 37 38 39;
41 42 43 44 45 46 47 48 49;
51 52 53 54 55 56 57 58 59]
Foe example I have a slope S1, and I want to select a band of this matrix in this way: Assume we can draw a line from array "6" to "29" and another line from "21" to "55". and remove the arrays above line 1 and below line 2. My problem is something like this but a little different. I have 2 slopes that I want to cut my matrix according with. Can you please help me to write a code for this problem?
0 个评论
采纳的回答
Andrei Bobrov
2012-5-28
[ii,jj]= ndgrid(1:size(a,1),1:size(a,2));
[I J] = find(ismember(a,[21;55;6;29]));
out = a.*(ii - I(1) > (diff(I(1:2)))/(diff(J(1:2)))*jj | ii - I(end) + 1< (diff(I(3:end)))/(diff(J(3:end)))*(jj - J(end)+1));
更多回答(1 个)
Image Analyst
2012-5-28
I don't know what you're trying to explain. I can get the elements fo the array going from 6 to 29 and from 21 to 55 this way:
a =...
[ 1 2 3 4 5 6 7 8 9;
11 12 13 14 15 16 17 18 19;
21 22 23 24 25 26 27 28 29;
31 32 33 34 35 36 37 38 39;
41 42 43 44 45 46 47 48 49;
51 52 53 54 55 56 57 58 59]
aLinear = a'
aLinear = aLinear(:)
a_6_29 = aLinear(6:27)
a_21_55 = aLinear(21:50)
% Now what? I have no idea.
But I don't know what to do after that. Do you want to get a slope of a line fitted through those vectors? What does this mean: "remove the arrays above line 1 and below line 2."? To me that means you want line (row) 2 and nothing else, like this:
% Remove the arrays above line 1 and below line 2.
% That would leave only line 2 itself remaining.
a_new = a(2, :);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!