简体   繁体   中英

Matlab: creating 3D arrays as a function of 2D arrays

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?

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 i th 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM