필터 지우기
필터 지우기

How do I find the indices of NaN values in a cell array?

조회 수: 31 (최근 30일)
Cai Chin
Cai Chin 2021년 1월 16일
댓글: Walter Roberson 2021년 1월 17일
Hi, I have a 3390 x 1 cell array containing 4 x 30 doubles. Some of these doubles contain NaN values which I would like to replace by the preceding double. How do I detect the indices of the doubles containing 'NaN' values and then replace them with the preceding 4x30 double? I have attached the cell array in question. Thanks in advance.

채택된 답변

Star Strider
Star Strider 2021년 1월 16일
Try this:
D = load('XTrain2_all.mat');
XTrain2_all = D.XTrain2_all;
hasNaN = cellfun(@nnz,cellfun(@isnan, XTrain2_all, 'Unif',0), 'Unif',0); % Cells With ‘NaN’ Values
idx = find([hasNaN{:}]); % Their Indices
XTrain2_all(idx) = XTrain2_all(idx-1); % Replace With Previous
hasNaN = cellfun(@nnz,cellfun(@isnan, XTrain2_all, 'Unif',0), 'Unif',0); % Check
Check = find([hasNaN{:}]); % Check
.
  댓글 수: 8
Cai Chin
Cai Chin 2021년 1월 17일
Hi, thank you very much for your suggestions. It is a possibility that there will be consecutive missing blocks, so is there any way of essentially scanning through the indices until the last non-NaN index is found and then replacing all the missing blocks with this?
Also, if there are multiple missing blocks from the first inde onwards, is it possible to scan forwards and replace the missing indices with the next non-NaN index values?
Star Strider
Star Strider 2021년 1월 17일
My pleasure!
It is a possibility that there will be consecutive missing blocks, so is there any way of essentially scanning through the indices until the last non-NaN index is found and then replacing all the missing blocks with this?
That would likely require looping through the ‘idx’ values. The first ‘idx’ value would be replaced with the matrix preceeding it, and the subsequent ‘idx’ values as well. It could end up that for consecutive ‘idx’ values, the same matrix could be duplicated consecutively as the result.
Also, if there are multiple missing blocks from the first inde onwards, is it possible to scan forwards and replace the missing indices with the next non-NaN index values?
That would likely require indexing in reverse, starting with the first full (non-NaN) cell matrix and going backwards. I have no idea how you would want to treat the rest of the array, whether going backwards from the last ‘idx’ value to the first would work, or if you would want to treat the various segments of the cell array differently.
In any event, all those possibilities would likely require a loop of some sort.
.

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 1월 16일
The below accounts for the possibility of multiple nan blocks.
It does not, however, account for the possibility that the first block is nan (there is no previous block to fill from in that case.)
hasnan = cellfun(@(C) any(isnan(C(:))), XTrain2_all);
idx = 1 : length(C);
idx(hasnan) = 1;
idx = fillmissing(idx, 'previous');
newC = C(idx);
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 1월 17일
편집: Walter Roberson 2021년 1월 17일
Corrected (tested)
load XTrain2_all.mat
hasnan = cellfun(@(C) any(isnan(C(:))), XTrain2_all);
idx = 1 : length(XTrain2_all);
idx(hasnan) = nan;
idx = fillmissing(idx, 'previous');
newXTrain2_all = XTrain2_all(idx);
Walter Roberson
Walter Roberson 2021년 1월 17일
To also account for the possibility of the first block being nan:
load XTrain2_all.mat
hasnan = cellfun(@(C) any(isnan(C(:))), XTrain2_all);
idx = 1 : length(XTrain2_all);
idx(hasnan) = nan;
idx = fillmissing(fillmissing(idx, 'previous'),'next');
newXTrain2_all = XTrain2_all(idx);

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by