Replace Nan by a number in a cell array

조회 수: 4 (최근 30일)
Susan
Susan 2019년 4월 26일
댓글: Susan 2019년 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
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.
Susan
Susan 2019년 4월 26일
Sorry, I didn't know that.
Sure thing!

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

채택된 답변

Guillaume
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
  댓글 수: 1
Susan
Susan 2019년 4월 27일
Thanks Guillaume for your reply. What you mentioned is exactly what I am doing and your answer is what I was looking for. Thanks again!

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

추가 답변 (2개)

Rik
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
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.
Susan
Susan 2019년 4월 27일
Thank you so much for your reply. It helps a lot.

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


Susan
Susan 2019년 4월 29일
Hey guys! cI got another question regarding cell arrays. Any help would be greatly appreciated.
I want to implement fmincon() for this problem:
I am minimizing an objective function with respect to some variables. the variable has the following format
X = cell(max(I(:)), numel(I), numel(I), numel(M), max(M(:))))
and inside each cell I have a Nt*Nr matrix. The goal is to find the optimal values of each matrix.
I defined the symbolic array of X (I am not sure if I am doing it correctly, though) and pass it to my objective function to calculate the objective function as follows:
X = cell2sym(cell(max(I(:)), numel(I), numel(I), numel(M), max(M(:))));
objfun = f(X)
but, I get the following error:
Brace indexing is not supported for variables of this type.
while I am able to get the value of f(X0) without any issue. Could anyone please kindly explain what the problem is?
Thanks in advance
  댓글 수: 2
Guillaume
Guillaume 2019년 4월 30일
You would be better off starting a new question. It's certainly not something I can help with as I don't have and know nothing about the symbolic toolbox.
Susan
Susan 2019년 4월 30일
Thanks for your reply. Sure I will.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by