Using cellfun to remove nans from uniform cell

6 次查看(过去 30 天)
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