Replace Nan by a number in a cell array
조회 수: 4 (최근 30일)
이전 댓글 표시
Hey MATLAB guys,
I would like to replace all Nan in the following cell array r by a number. Could anyone please tell me how I can do that for the following problem? Thanks in advance
r = cell(sum(nL), numel(nL), numel(nW), max(nW(:)));
for k = 1 : numel(nW)
for m = 1 : nW(k)
for j = 1 : numel(nL)
for i = 1 : nL(j)
r{i + Nup*(j - 1), j, k, m} = .....
end
end
end
end
댓글 수: 2
Rik
2019년 4월 26일
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
채택된 답변
Guillaume
2019년 4월 26일
Your question is not very unclear. You show a loop filling (part) of a cell array with ... something unspecified. Presumably, that something is a matrix or vector of varying size and not a scalar (otherwise you wouldn't be using a cell array) and maybe part (all?) of it can be NaN.
If you don't want NaNs in the something, simply replace them before copying the something in the cell array:
for ...
for ...
something = ...
something(isnan(something)) = somevalue; %replace NaNs by somevalue
r{..} = something;
end
end
추가 답변 (2개)
Rik
2019년 4월 26일
You can use the isnan function:
r = cell(sum(nL), numel(nL), numel(nW), max(nW(:)));
for k = 1 : numel(nW)
for m = 1 : nW(k)
for j = 1 : numel(nL)
for i = 1 : nL(j)
if isnan(r{i + Nup*(j - 1), j, k, m})
r{i + Nup*(j - 1), j, k, m} = .....
end
end
end
end
end
댓글 수: 4
Rik
2019년 4월 27일
That seems like it should work, yes. I just wrote the code like that because you didn't mention any context of your question, so it was/is a bit difficult to give sensible advice. You can also have a look at Guillaume's suggestion, maybe that suggestion works better for your situation.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!