Save multiple 2d-Arrays with different amount rows in the same 3d-Array

I wat to save differet 2d-Array in one 3d-Array
A =
1 1 1
1 1 1
1 1 1
B =
2 2 2
2 2 2
2 2 2
2 2 2
Array3d(:,:,1) = A(:,:)
Array3d(:,:,2) = A(:,:)
the resault is this message:
Unable to perform assignment because the size of the left side is 3319-by-7 and the size of the right
side is 1574-by-7.
I agree that they are not the same size, but i'm searching for a way how to size up the first (and several previous) "2d-Layers" of the 3d-Array.
For example i want that the Array-"Layer" Array3d(:,:,1) gets tranformed to this
Array3d(:,:,1) =
1 1 1
1 1 1
1 1 1
0 0 0
I expected a way of self extension like in this case
>> C(3,2)= 1
C =
0 0
0 0
0 1
I don't have to define the zeros...
Would be very thankfull for any help.

 Accepted Answer

In case you know already know which matrix have more rows, you can do something like the following
A; % 3319x7
B; % 1574x7
n = size(A,1);
B(n,:) = 0;
Array3d = cat(3, A, B)
The line B(n,:) = 0; will automatically expand B to have same number of rows as A, and fill the new rows with 0.

4 Comments

Thank you for the quick response!
That's a way to to it, thank you!
In my specific case the 2d-Arrays get generated and i want to save them in a 3d-Array befor generating the next one.
I will have several 2d-arrays wich are smaler or bigger than the existing "2d-Layer" of the the 3d-array. I hoped that there is a more automatic way to solve problem.
But it works! Thank's again :)
for j=1:nphi
for i=1:sz(1)
if (j-1)*phi < wind(i,6) && wind(i,6) < j*phi
k = k +1;
windphi2d(k,:) = wind(i,:);
sz2d = size(windphi2d);
end
if sz2d(1) < sz3d(1) %Größe des 2D-Array anpassen
windphi2d(sz3d,:)=0;
elseif sz2d(1) > sz3d(1)
windphi(sz2d(1),:,l)=0;
end
end
windphi(:,:,l) = windphi2d(:,:); %2D-Array in 3D-Array einfügen
sz3d = size(windphi); %Größe des 3D-Array erfasse
l= l+1; %Zähler für 3D-Array "Lage" erhöhen
k=0; %2D-Array Zähler nunnel
windphi2d(:,:)=0; %2D-Array leeren
end
So everytime there is a new 2D array larger the the previous arrays, you want to expand the complete 3D array to fit the 2D later?
Try this
for j=1:nphi
for i=1:sz(1)
if (j-1)*phi < wind(i,6) && wind(i,6) < j*phi
k = k +1;
windphi2d(k,:) = wind(i,:);
sz2d = size(windphi2d);
end
if sz2d(1) < sz3d(1) %Größe des 2D-Array anpassen
windphi2d(sz3d,:)=0;
elseif sz2d(1) > sz3d(1)
windphi(sz2d(1),:,l)=0;
end
end
if size(windphi2d,1) > size(windphi,1)
n = size(windphi2d,1);
windphi(n,:,:) = 0;
elseif size(windphi2d,1) < size(windphi,1)
n = size(windphi,1);
windphi2d(n,:) = 0;
end
windphi(:,:,l) = windphi2d(:,:);
sz3d = size(windphi); %Größe des 3D-Array erfasse
l= l+1; %Zähler für 3D-Array "Lage" erhöhen
k=0; %2D-Array Zähler nunnel
windphi2d(:,:)=0; %2D-Array leeren
end

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!