Replace Nan by a number in a cell array
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 26 Apr. 2019
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.
Akzeptierte Antwort
Guillaume
am 26 Apr. 2019
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
Weitere Antworten (2)
Rik
am 26 Apr. 2019
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 Kommentare
Rik
am 27 Apr. 2019
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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!