Replacing Values in Matrix with NaN's based on Row and Column

28 visualizaciones (últimos 30 días)
Hi everyone!
I hope all of you are well. I'm currently working on replacing specific element values with NaN's within a matrix that is 300x600. I want to replace the values based on pairs of rows and columns that I have previously identified. I'm unsure if I would use a for loop or if I would have to create a mask or if there is a simpler way of approaching the problem, but any help would be greatly appreciated! Thank you in advance! :)
M.

Respuesta aceptada

madhan ravi
madhan ravi el 24 de Jun. de 2020
Editada: madhan ravi el 24 de Jun. de 2020
matrix(ismember(matrix, [3,7,1])) = nan % an example of replacing numbers 3,7 & 9 with nans
% or
matrix = rand(5); % an example
rows = [2, 5];
columns = [1, 4];
idx = rows + (columns - 1) * size(matrix, 1);
matrix(idx) = nan

Más respuestas (1)

Tommy
Tommy el 24 de Jun. de 2020
Possibly, you could use sub2ind() to convert your rows and columns to linear indices:
ind = sub2ind(size(yourMatrix), yourRows, yourCols);
yourMatrix(ind) = NaN;
  1 comentario
Michelle De Luna
Michelle De Luna el 24 de Jun. de 2020
Tommy, thank you for your speedy response! I've accepted the second response but appreciate your help.

Iniciar sesión para comentar.

Categorías

Más información sobre NaNs en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by