ekrem777
ekrem777

Reputation: 45

Matlab 3D matrix extension

I have 2x2x1 3d matrix (val) in MATLAB such:

val(:,:,1) =

   195   1386
   27   10

val(:,:,2) =

   196   138
   217   102

and I want to add

val(:,:,3) = 

   196   138
   217   102
   217   102.

As usual, Matlab gives dimension mismatch error. What should I do to my val matrix before adding 3rd (3x2) matrix?

Upvotes: 0

Views: 120

Answers (2)

G.J
G.J

Reputation: 795

Your array cannot have empty spots.

For changing dimensions, you should consider using struct or cell.

Upvotes: 0

Luis Mendo
Luis Mendo

Reputation: 112659

Given your original array:

val(:,:,1) = [
   195   1386
   27   10 ];
val(:,:,2) = [
   196   138
   217   102 ];

you need to define a third row. Fill it with something, for example with NaN:

val(end+1,:,:) = NaN;

That way, dimensions will match when you add the new slice:

val(:,:,3) = [
   196   138
   217   102
   217   102 ];

Upvotes: 1

Related Questions