开发者

Tiling matrices in matlab

开发者 https://www.devze.com 2023-01-05 17:31 出处:网络
Here\'s an interesting question :) I have two \"vectors of matrices\" which I want to tile like the hankel funct开发者_JAVA技巧ion does for regular vertices.

Here's an interesting question :)

I have two "vectors of matrices" which I want to tile like the hankel funct开发者_JAVA技巧ion does for regular vertices. For example: Column Vector:

10
00

20
00

30
00

Row vector:

30 40 50 60
00 00 00 00

The resulting matrix needs to be:

10 20 30 40
00 00 00 00

20 30 40 50
00 00 00 00

30 40 50 60
00 00 00 00

Note that the 0 values can be changed, the resulting structure is the important part.

A related question: I looked in the command "edit repmat" and saw some interesting syntax I couldn't find help for:

A=[1,3;2,4];
X=[1,1;2,2];
B=A(X,X);

and B ends up being

1 3 1 3
2 4 2 4
1 3 1 3
2 4 2 4

which is basically repmat(A,2,2);

So my question is, what is this syntax: A(X,X)?

Thanks a lot!

Ofer


If you want to tile a set of matrices the way HANKEL tiles values, here's one way you can do it. First, you can put all of your unique matrices in one cell array:

mat = [1 0; 0 0];
cArray = {mat 2.*mat 3.*mat 4.*mat 5.*mat 6.*mat};  %# Your 6 unique matrices

Now, if you want the first 3 matrices running down the first column and the last 4 matrices running across the last row, you can create an index matrix using HANKEL:

>> index = hankel(1:3,3:6);

index =

     1     2     3     4
     2     3     4     5
     3     4     5     6

Then index your cell array with index and use CELL2MAT to convert the resulting cell array to one matrix:

>> cell2mat(cArray(index))

ans =

     1     0     2     0     3     0     4     0
     0     0     0     0     0     0     0     0
     2     0     3     0     4     0     5     0
     0     0     0     0     0     0     0     0
     3     0     4     0     5     0     6     0
     0     0     0     0     0     0     0     0



For the second part of your question, when you perform an indexing operation like A(X,Y), you are using the elements of X as row indices and the elements of Y as column indices into A. Every combination of values in X and Y is used. So, if X = [x1 x2 x3 x4] and Y = [y1 y2 y3 y4], then the result of B = A(X,Y) is equivalent to:

B = [A(x1,y1) A(x1,y2) A(x1,y3) A(x1,y4); ...
     A(x2,y1) A(x2,y2) A(x2,y3) A(x2,y4); ...
     A(x3,y1) A(x3,y2) A(x3,y3) A(x3,y4); ...
     A(x4,y1) A(x4,y2) A(x4,y3) A(x4,y4)];
0

精彩评论

暂无评论...
验证码 换一张
取 消