select a band of a matrix
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Andrei Bobrov
on 28 May 2012
[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));
More Answers (1)
Image Analyst
on 28 May 2012
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, :);
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!