How to create a 2x2 matrix out of 4 matrices?

I have 3 matrices, each of size 1x100000 and the fourth element is a single numerical value. I am trying to make a square matrix 'Z' of size 2x2 which shall contain each of these 4 elements and do some operations on its inverse.
Could you please tell how I can do that?

5 Comments

How can a matrix with 4*1000000 elements be size 2x2?
Please be clear and provide a small example.
How do you want to reduce the number of elements? Which of the resulting 400.000 elements should be used to create the wanted 4 elements??
Ananth, moving your comments here:
Ok, I want the final matrix of size 2x2 to contain 4 elements, each with 1x100000 values. Then, take its inverse. Could I do that by padding the 4th element with zeros to make the dimensions equal to the other 3 sub-matrices? The context is impedance calculations. The 100000 values are those multiplied by frequency points.
the cyclist says:
It's still not 100% clear to me what you want. Do you expect, in effect, to be taking the inverse of 100,000 2x2 matrices?
Ananth says:
I was trying to comment but instead pressed 'accept'. Cyclist, you are right. I am trying to take the inverse of the 100,000 2x2 matricces. But, the problem is inverses can be taken only on square matrices. I am unsure how to tackle this. Any suggestions?

Sign in to comment.

 Accepted Answer

After you create z as above, then a simple loop can calculate the inverse over the third dimension:
>> invz = zeros(2,2,100000);
>> for zi = 1:100000,
>> invz(:,:,zi)=inv(z(:,:,zi));
>> end
Don't neglect the preallocation of memory in that first line.
I feel like there may be a may to do all the inverse in one fell swoop (with accumarray?), but it's not coming to me. The loop above only takes a couple seconds on my machine.

2 Comments

That hit the spot. Thank you Cyclist!!
Ananth - could you please mark this answer as accepted, so that other MATLAB Answers readers will know that this is a solved problem? Thanks!

Sign in to comment.

More Answers (2)

It's not entirely clear to me what you want the output matrix to look like, so this is a guess. Using Paulo's notation for your input arrays, then
>> z = reshape([mat1; mat2; mat3; mat4], [2 2 100000]);
will give you a 2x2x100000 array where each "slice" is a 2x2, with one element from each of the input matrices. Is that what you want?

Categories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!