Index 2D matrix along line rotated about the matrix midpoint
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I have an N x N matrix that I would like to sequentially index along a line that slices the middle of the matrix and is then rotated by 180/(2(N-1)) degrees until it completes half a full rotation. In the end, we want to end up with 2(N-1) distinct lines. Picture attached for a 5x5 case for clarity, since it's easier to describe by a picture than by words, I think. If N were even, then the "equivalent" center point of the matrix could be arbitrarily decided by ceil(N/2).
So for example, if the matrix is:
a = [1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]
Then the elements returned for each iteration would be the middle row, middle column, diagonals as well as:
[2 13 24]
[4 13 22]
[6 13 20]
[10 13 16]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241880/image.png)
The only way I can think of implementing this is to rotate the 2D matrix by the angle 180/(2(N-1)) and then always slice the middle column of the matrix but I'm not sure this is the most optimal way to do so for large matrices. Is there any other, more efficient way to achieve this?
3 comentarios
Daniel M
el 9 de Oct. de 2019
Sorry I can't help because what you're asking for is still unclear. You have not been able to specify what you want in the general case. My post above still applies regarding if N = 7 (let alone if N = 99 or higher), or if N is even. What you want is unknown, so how can I help?
Respuestas (2)
Daniel M
el 9 de Oct. de 2019
Editada: Daniel M
el 9 de Oct. de 2019
360/(N-1) doesn't make sense. What if N = 8? I think you just mean 180. But it's irrelevant. It would not be efficient to recreate the matrix by shifting the elements around.
It seems all you want to do is get the middle column, middle row, and the diagonals. Well that is simple enough for odd N (you can provide more clarity for even-numbered N if you wish).
A = magic(5);
sz = size(A,1);
mid = ceil(sz/2);
midcol = A(:,mid);
midrow = A(mid,:);
maindiagonal = diag(A);
oppositediagonal = A(sz:sz-1:end-1);
Other than that I'm not sure what order or arrangement you want the values it, but it should be pretty straightforward from here. Use commands like flip and transpose.
3 comentarios
Daniel M
el 9 de Oct. de 2019
Those are an odd pairing of indices for sure. I hope when you rewrite your question, you pose it in a very clear way that generalizes it for a matrix of size N.
Daniel M
el 20 de Oct. de 2019
Here is another idea, perhaps you can play with it. You'll have to implement this in a loop.
[cx,cy,c] = improfile(I,xi,yi,n);
xi are the line endpoints x values. yi are the endpoints y values. n is the number of samples along the line that you want to extract. c are those image values, and cx,cy are the exact points where the measurements were made.
For example, the green line in your image:
x = magic(5);
[cx,cy,c] = improfile(x,[2,4],[1 5],3)
0 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!