From: Avneep Dhanju on
I just found out about the find() function but the approach I was planning with it is not so efficient as it will get very cumbersome to find the 2X2 neighborhood from the vectors.

And by the way, I just understood your second approach. Wow. I would have never been able to come up with something using tensor product. This works absolutely fine. Again wow. And thanks a lot.

But now the problem is I have to write another function that does the opposite of this. You see I will be using these functions in a program which implements a multigrid method. So this reduce function implements the restriction part of the multigrid method. Now I need an expand function which implements the interpolation part. The expand function gives a matrix with size twice as that of the input matrix and each element of the input matrix is copied to the 2X2 neighborhood of the output matrix.

Any leads on this one?
From: Avneep Dhanju on
Well I think I got it using the tensor product. This works just fine:

function [q] = expand(p)
[m,n]=size(p);
D=kron(speye(m),[1;1]);
q=(D*p)*D.';

Thanks Matt. Thanka a lot.
From: Matt J on
"Avneep Dhanju" <avneepdhanju(a)gmail.com> wrote in message <hi3vrn$3ij$1(a)fred.mathworks.com>...
> Well I think I got it using the tensor product. This works just fine:
>
> function [q] = expand(p)
> [m,n]=size(p);
> D=kron(speye(m),[1;1]);
> q=(D*p)*D.';
>
> Thanks Matt. Thanka a lot.

No problem. Bear in mind though that the original approach I proposed based on reshape() is what you would want to do with smaller, non-sparse matrices.

The tensor product approach is only a matter of necessity in the case of large sparse A because reshaping these can run into memory problems.