Borrar filtros
Borrar filtros

How to add NaN values to a matrix?

134 visualizaciones (últimos 30 días)
Yelitza Perez
Yelitza Perez el 13 de Nov. de 2020
Comentada: Cris LaPierre el 14 de Nov. de 2020
Hi, I need to add NaN values to two matrices. One is a 1x48000 and it needs to be 1x48449 and the other is 4x48000 and it needs to be 4x48449. How can I do that? I need to add these extra values because the 1x48000 matrix is beign concatinated with another matrix of size 1x48449. So I was thinking to add some NaN values or maybe substract the 449 extra values from the second matrix.
Can anyone help me understand this better? Thank you for any help you can provide.

Respuestas (2)

Cris LaPierre
Cris LaPierre el 13 de Nov. de 2020
Use the missing function.
% Define a 4x5 matrix
A=ones(4,5);
% Turn into a 4x9
A(:,end+1:9)=missing
A = 4×9
1 1 1 1 1 NaN NaN NaN NaN 1 1 1 1 1 NaN NaN NaN NaN 1 1 1 1 1 NaN NaN NaN NaN 1 1 1 1 1 NaN NaN NaN NaN
  2 comentarios
Yelitza Perez
Yelitza Perez el 14 de Nov. de 2020
Thank you! I like your answer. But how would this work if I already defined the matrix in a previous line? (I had to change the name because I have two matrices on my workspace with the same name)
Cris LaPierre
Cris LaPierre el 14 de Nov. de 2020
That's what my code does. I first defined the matrix A, then I increased its size by adding NaNs.
It would appear you know the size of your matrices, so adapt the code I shared to increase the size of your smaller matrix so that it is the size of the larger one.

Iniciar sesión para comentar.


Setsuna Yuuki.
Setsuna Yuuki. el 13 de Nov. de 2020
Editada: Setsuna Yuuki. el 13 de Nov. de 2020
A = rand(1,48000) %Matrix of example (1x48000)
B = rand(1,48449) %Matrix of example (4x48449)
C = [A;B(1:48000)] %You can take the matrix values from 1 to 48000, and concatenate.
and
A = rand(4,48000) %Matrix of example (4x48000)
B = rand(4,48449) %Matrix of example (4x48449)
C = [A;B(1:4,1:48000)] %You can take the matrix values from 1 to 48000, and concatenate.
  2 comentarios
Yelitza Perez
Yelitza Perez el 14 de Nov. de 2020
Thanks for this answer! Would the 'rand' part generate random values on a 1x48000 matrix? Because my 1x48000 already have values. I don't want to overide them, I just want to add 449 NaN more values at the end so it could work when I concatenate it with my other 1x48449 matrix.
Sorry I am new at this.
Setsuna Yuuki.
Setsuna Yuuki. el 14 de Nov. de 2020
A and B are your matrix, rand is a method to create random data, but is only a example.
A; %your 1x48000 matrix
B; %your 1x48449 matrix
C=[A B(1:48000)] %concatenate A matrix and B matrix, but only from element 1 to element 48000 of B matrix
This method removes values from matrix B does not add NaN in matrix A

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by