How to extract values from a matrix along a certain line (not vertical or horizontal).
5 次查看(过去 30 天)
显示 更早的评论
Assume I have the following matrix:
A=[11 13 43 53 66 45 44 83 91;
23 43 54 65 76 78 76 98 33;
23 54 65 76 43 16 38 27 65]
I want to extract values from this matrix, along 45 degrees, to form a new matrix
B=[11 13 43 53 66 45 44;
43 54 55 76 78 76 98;
65 76 43 16 38 27 65]
How I can do it in MATLAB? Thanks.
0 个评论
回答(2 个)
Image Analyst
2015-10-25
If you have the Image Processing Toolbox, simply use improfile().
2 个评论
Image Analyst
2015-10-25
Or, call imrotate() to align the image with rows or columns, and extract a row or column. Should be pretty close to the same thing.
Image Analyst
2015-10-25
improfile() will extract along a line at an arbitrary angle - it doesn't have to be exactly 45 degrees. You just specify the two endpoints of the line and how many points along that line you want to interpolate.
Jan
2015-10-25
A = [11 13 43 53 66 45 44 83 91; ...
23 43 54 65 76 78 76 98 33; ...
23 54 65 76 43 16 38 27 65];
[m, n] = size(A);
index = true(m, n);
index = tril(triu(index), n - m);
B = reshape(A(index), m, [])
2 个评论
Image Analyst
2015-10-25
Doesn't give what he wanted, but I'm not sure how he got that anyway. For example it sort of appears that it's a rotation about the middle element, but not quite. Because using
B=imrotate(A, -45, 'crop')
will give
B =
0 54 65 65 53 66 0 0 0
0 0 76 76 76 45 45 0 0
0 0 0 43 16 78 44 83 0
while Jan's code gives:
B =
11 43 53 66 45 44 98
13 54 65 76 78 76 27
43 65 76 43 16 38 65
Neither of which is
B=[11 13 43 53 66 45 44;
43 54 55 76 78 76 98;
65 76 43 16 38 27 65]
which the user asked for. So I need Peng to explain how B was arrived at. Also, Jan's code is for 45 degrees, which is the example angle Peng wanted, but in the subject line Peng asked for an "Arbitrary angle:. So which is it?? 45 or arbitrary? And if it's just a rotation, then why is B two columns (but no rows) shorter? If anything a rotated matrix would be larger, and you can get that with the 'loose' option to imrotate() where it will expand the canvass to hold the entire rotated matrix.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!