Create a 4D array from 3D array with 3 columns, and 1D (row only) array with 3 columns
Afficher commentaires plus anciens
When I load a test data file, Matlab tells me it has 4 dimensions, and when I check size, I see:
>> size(myVar)
ans =
3 241 161 161
'3' because I think the variable contains a 3D array.
'241 161 161' because the variable also contain a 1D array that has one row with three columns.
(Or '241 161 161' could mean there are three, 1D arrays with one column in each? I don't know how to check.)
Either way, I know that 241, 161, 161 is the dimension of a space in a 3D grid-like structure.
I want to create a .mat file with similar structure from the following two arrays:
1st array:
I have a 3D array, with size {576 3 4}, it looks something like,
(:,:,1) =
-0.0336 0.6440 -0.1464
-0.0336 0.6441 -0.1467
...... ....... .......
576 rows.
Also similar structure for (:,:,2), (:,:,3) and (:,:,4).
2nd array:
1D, row-only array with three columns
L = [ 100 100 100];
How do I create a 4D variable that has data from arr1 and arr2, and looks like what I wrote in the beginning?
My guess is its size would look like '3 100 100 100'.
The .mat creating part is the easy part:
save('myFile.mat','4D_var');
But I don't know how to create that 4D_var.
Thank you.
4 commentaires
'3' because I think the variable contains a 3D array.
'241 161 161' because the variable also contain a 1D array that has one row with three columns.
No, neither are true. If the size(myVar) reports [3 241 161 161]. It is because it contains one array only, which is a 4D array of dimension 3x241x161x161. Accordingly, the number of elements myVar holds contains is,
numElements=3*241*161*161
Nx = length(myVar(1,:,1,1)); gives 241 ... So, could it be that myVar has 4 arrays inside it?
No.
What would you say about myVar(2,:,3,5)? You will see that length(myVar(2,:,3,5)) is also 241. Wouldn't that imply, by your reasoning, that myVar also contains a 5th array which is not in your list above and which also has 241 "rows"?
More generally, you will see that any myVar(i,:,j,k) gives a different length-241 vector for any combination of (i,j,k). There are many more than 4 such combinations.
Jartok
le 15 Jan 2024
Réponse acceptée
Plus de réponses (2)
If understood correctly, this is what you are trying to get, e.g.:
rng(13) % for reproducibility
A = rand(5, 3);
size(A)
% B and C have the same size as A
B = rand(5, 3);
C = rand(5, 3);
L(:,:, 1) = A;
L(:,:, 2) = B;
L(:,:, 3) = C;
size(L)
% D array
D = rand(5, 3)
L(:,:,4) = D;
% Final 4-D array
[Row, Col, Layer]=size(L)
L
1 commentaire
Jartok
le 15 Jan 2024
So, could it be that myVar has 4 arrays inside it
To me, it doesn't sound like you are talking about a 4D array at all, but instead a cell array. A cell array is a variable that can have "4 arrays inside it", for example:
xyz=rand(576,3,4);
Bx=rand(100,100,100);
By=rand(100,100,100);
Bz=rand(100,100,100);
whos xyz Bx By Bz
myVar={ xyz, Bx,By,Bz}
Here the first array is a 3D array of dimension 576x3x4 and the second,third, and fourth arrays are each 100x100x100
size(myVar{1})
size(myVar{2})
size(myVar{3})
size(myVar{4})
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





