yankeefan11
yankeefan11

Reputation: 485

Matrix at each point of mesh

So I have a mesh grid

x = linspace(0,1,250);
y=x;
[X,Y] = meshgrid(x,y) ;

At each point, I want a 2x2 matrix (that I will find eigenvalues for). But I cannot figure out the best way (short of looping through the whole 250x250 space).

Suppose my matrix for each X,Y was

M(1,1) = X
M(2,2) = Y
M(1,2) = sin(2*pi*X)
M(2,1) = X.*Y.^2; 

What would be the best way to do this then get the eigenvalues for each X,Y?

Upvotes: 2

Views: 65

Answers (1)

bla
bla

Reputation: 26069

Not sure by what you mean "best", fastest? here is a simple way to do that if I understood what you wanted (2 eigen-values per pixel):

Start with your definitions

x = linspace(0,1,250);
y=x;
[X,Y] = meshgrid(x,y) ;
S=sin(2*pi*X);
XY2=X.*Y.^2; 

Then, we can use linear indexing:

for n=1:numel(X)
    M(:,n)=eig([X(n), S(n); XY2(n) , Y(n)]);
end

That's it, all the information is in M... This took ~3 seconds on my laptop. If you want faster implementation of the eig function for the case of 2x2 matrices you can use this. If you want to go back from linear indexing to the 2D (i,j) index, you can use this to get for pixel i,j the eigenvalues:

M(:, sub2ind(size(X),i,j))

Upvotes: 2

Related Questions