1

I want to create 3d arrays that are functions of 2d arrays and apply matrix operations on each of the 2D arrays. Right now I am using for loop to create a series of 2d arrays, as in the code below:

for i=1:50
    F = [1 0 0; 0 i/10 0; 0 0 1]; 
    B=F*F';
end

Is there a way to do this without the for loop? I tried things such as:

F(2,2) = 0:0.1:5;

and:

f=1:0.1:5;
F=[1 0 0; 0 f 0; 0 0 1];

to create them without the loop, but both give errors of dimension inconsistency.

I also want to perform matrix operations on F in my code, such as

B=F*F';

and want to plot certain components of F as a function of something else. Is it possible to completely eliminate the for loop in such a case?

2
  • From your samples it is unclear on what is the size of your 3-D array and how you assign each element. Can you add indexes to the loop example you have given and show how you are creating 3-D array with loops now? Commented Aug 27, 2015 at 14:53
  • I am only creating 2D arrays right now. My loop index is in a way the third index right now, as I am creating a series of 2D arrays using the loop. Commented Aug 27, 2015 at 15:20

2 Answers 2

4

If I understand what you want correctly, you want 50 2D matrices stacked into a 3D matrix where the middle entry varies from 1/10 to 50/10 = 5 in steps of 1/10. You almost have it correct. What you would need to do is first create a 3D matrix stack, then assign a 3D vector to the middle entry.

Something like this would do:

N = 50;
F = repmat(eye(3,3), [1 1 N]);
F(2,2,:) = (1:N)/10; %// This is 1/10 to 5 in steps of 1/10... or 0.1:0.1:5

First pre-allocate a matrix F that is the identity matrix for all slices, then replace the middle row and middle column of each slice with i/10 for i = 1, 2, ..., 50.

Therefore, to get the ith slice, simply do:

out = F(:,:,i);

Minor Note

I noticed that what you want to do in the end is a matrix multiplication of the 3D matrices. That operation is not defined in MATLAB nor anywhere in a linear algebra context. If you want to multiply each 2D slice independently, you'd be better off using a for loop. Doing this vectorized with native operations isn't supported in this context.

To do it in a loop, you'd do something like this for each slice:

B = zeros(size(F)); 
for ii = 1 : size(B,3) 
    B(:,:,ii) = F(:,:,ii)*F(:,:,ii).'; 
end

... however, examining the properties of your matrix, the only thing that varies is the middle entry. If you perform a matrix multiplication, all of the entries per slice are going to be the same... except for the middle, where the entry is simply itself squared. It doesn't matter if you multiple one slice by the transpose of the other. The transpose of the identity is still the identity.

If your matrices are going to be like this, you can just perform an element-wise multiplication with itself:

B = F.*F;

This will not work if F is anything else but what you have above.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I wanted to define the matrices in this manner. But, as I said, is it possible to perform matrix operations on F without using the for loop? From what it seems to me, I'll still need a for loop to run through all slices to compute, say for instance, B=F*F'.
@Champ - No. Not that I know of. MATLAB does not have support for this operation. You'd have to loop and do it for each slice. Something like: B = zeros(size(F)); for ii = 1 : size(B,3), B(:,:,ii) = F(:,:,ii)*F(:,:,ii).'; end. You don't have a choice but to loop I'm afraid.
@Champ - If your matrix is going to be like the identity but with the middle entry changing, then you can simply calculate the element-wise multiplication of each element. See my edit.
That's a good point. I can use it for preliminary versions of the code, but later on, F may get complicated and I'll have to get to the for loop eventually.
@Champ - That's right. It all depends on how F is structured... but if it'll eventually evolve to more random elements per slice, then the loop is unfortunately the only way to go. Recent versions of MATLAB exploit the JIT acceleration as MATLAB uses the JVM as the backbone, so you may be surprised with how fast it can compute the loop in comparison to trying to use more built-in functions. All I can say is give it a try and see how it goes!
3

Creating the matrix would be easy:

N = 50;
S    = cell(1,N);
S(:) = {eye(3,3)};
F        = cat(3, S{:});
F(2,2,:) = (1:N)/10;

Another (faster) way would be:

N = 50;
F        = zeros(3,3,N);
F(1,1,:) = 1;
F(2,2,:) = (1:N)/10;
F(3,3,:) = 1;

You then can get the 3rd matrix (for example) by:

F(:,:,3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.