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

조회 수: 26 (최근 30일)
Michelle De Luna
Michelle De Luna 2020년 6월 24일
댓글: Michelle De Luna 2020년 6월 24일
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.

채택된 답변

madhan ravi
madhan ravi 2020년 6월 24일
편집: madhan ravi 2020년 6월 24일
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

추가 답변 (1개)

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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by