필터 지우기
필터 지우기

Using cellfun to remove nans from uniform cell

조회 수: 6 (최근 30일)
James Spalding
James Spalding 2022년 10월 19일
편집: dpb 2022년 10월 19일
Hi Guys, I have a bunch of cell values continaing 17 unifrom sized matrices that have NaN values in them. I need to replace these NaNs with any large place-holder number. I am trying to use a solution that has shown up in other posts:
https://www.mathworks.com/matlabcentral/answers/440856-replacing-nans-with-zero-in-a-matrix-within-a-cell-array
https://www.mathworks.com/matlabcentral/answers/459133-how-to-replace-nan-values-with-any-other-values-in-a-cell-array
Here is my code
RvarNnan1 = cellfun(@(M) subsasgn(M, substruct('()', {isnan(M)}), 123000), RvarNnan1, 'UniformOutput', 123000);
I am getting the following errors, prior to running:
"output must be assigned to a variable"
and after running get :
"Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false."
I think i don't understand the "M" variable. I've attached my cell variable(this is a sliced version {5x1}, of the actual variable {17x1} due to upload size restraits. Also of note, different from the other posts, I am replacing the NaNs with a meaningless large number. I have tried switiching around M with a predifenedvariable as well as using the actual VarNnan1.
Any help here is greatly appreciated!
  댓글 수: 1
James Spalding
James Spalding 2022년 10월 19일
Resolved it myself. I needed to change the final field to 'false' and it ran correctly.
here's what the code looks like now.
RvarNnan1 = cellfun(@(M) subsasgn(M, substruct('()', {isnan(M)}), 123000), RvarNnan1, 'UniformOutput', false);

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

채택된 답변

dpb
dpb 2022년 10월 19일
If they are and always will be the same size, I'd use subterfuge...
[ra,ca]=size(R{1}); % save the array sizes first...
[r,c]=size(R); % and the cell array size
R=cell2mat(R);
R(inan(R))=0;
R=mat2cell(R,r*ones(ra,1),c*ones(ca,1)); % turn back to cell array
  댓글 수: 3
James Spalding
James Spalding 2022년 10월 19일
Thanks for the feedback!
And yeah I would prefer to keep the nans but the next step for me is using Interp2 which can't have NaN values, but i need to maintain the dimensions of the arrays for the cordinates to match to the cordinates variables. I get around wildly wrong interpolated values using the 'nearest' option.
dpb
dpb 2022년 10월 19일
편집: dpb 2022년 10월 19일
Oh. In that case I'd look at fillmissing instead...you could use one of the interpolation methods there instead and have "ready for prime time" data array for interp2. Or, if it is just using interp2 to fill the missing rather than actually interpolating to a finer grid, you've saved a step entirely.
Athough fillmissing only operates over one dimension; it can interpolate either by row or by column (default) but, won't/can't do a 2D interpolation over the surrounding values.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by