How to create a 3D array in matlab?

Possible duplicate:
Matlab: how to create a 3D matrix?

I have 2 vectors:

A= 5 x 10 ( 5 rows, 10 column) B= 5 x 6 ( 5 rows , 6 column) 

How to create a 3D array, i.e. 5 x 10 x 6 , but not filled with zeros?

+4
source share
1 answer

I don’t think you need a 3D matrix. I would suggest the following options:

Option 1. Cell Cell

 >> A = rand(5,10); B = rand(5,6); >> {A, B} ans = [5x10 double] [5x6 double] 

Option 2. Cell Cell

 >> clear C; for i=1:5, C{i,1} = A(i,:)'; C{i,2} = B(i,:)'; end; C C = [10x1 double] [6x1 double] [10x1 double] [6x1 double] [10x1 double] [6x1 double] [10x1 double] [6x1 double] [10x1 double] [6x1 double] 

Option 3. Concentrate the arrays, and then just index the parts that you need.

 >> C = [A,B]; size(C) ans = 5 16 

Option 4. Use a structural array

 >> clear C; for i=1:5, C(i).A = A(i,:)'; C(i).B = B(i,:)'; end; C C = 1x5 struct array with fields: A B 

If you think of a 3D matrix as a cube and a 2D matrix as a square, you should see that what you are trying to do is create a cube of two squares, leaving the cube volume empty (just like the other four sides).

+1
source

Source: https://habr.com/ru/post/1383159/


All Articles