How can I add NaNs to a cell array in positions that match the positions of NaNs in a separate matrix?

1 visualización (últimos 30 días)
I have a matrix (A) containing some NaNs. I also have a cell array (B) with no NaNs, containing script in every element.
Where NaNs exist in A, I also want NaNs to appear in B.
Below is my failed attempt to do this. The final line of the script results in an error: "Conversion to cell from double is not possible." Any advice would be much appreciated.
A = rand(2); % matrix
A(1,2) = NaN
B = {'fred' 'wilma'; % cell array
'barney' 'betty'};
nan_locations = find(isnan(A)); % index location of NaNs in A
B(nan_locations) = NaN % in B insert NaNs in same locations as A

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 5 de Dic. de 2012
B(isnan(A)) = {NaN}

Más respuestas (1)

Vishal Rane
Vishal Rane el 5 de Dic. de 2012
Use
B{isnan(A)} = NaN
  5 comentarios
Vishal Rane
Vishal Rane el 5 de Dic. de 2012
Above method will give error if there are no NaN values in A.
Post your code if possible.

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations 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