How declare for the matlab matrix that an value don't is considerable ??
显示 更早的评论
How declare for the matlab matrix that an value don't is considerable?
I'm opening netcdf data and I want that the matlab ignore a specific value.
How do I?
Thanks! Carlos
采纳的回答
更多回答(3 个)
dpb
2014-7-11
a=a(a~=2);
or
a(a==2)=[];
will replace a. If need to keep a for future reference, use the first form but assign RHS to another variable. NB: if the values aren't integers, floating point comparisons for exact values are not always reliable, you'll need to use "fuzzy" comparison or otherwise account for it.
dpb
2014-7-11
The size of the array has no bearing on the logic.
What I said, save it to another...
b=a(a~=2);
Or, there's the alternative of just using "logical addressing" directly without actually making a copy.
res_notwos=mean(a(a~=2)); % mean excluding elements equal 2
Or another alternative is
a(a==2)=nan; % nan as placeholder
res=nanmean(a); % Stat Toolbox has several such "NaN-aware" functions
The latter is particularly convenient when plotting as plot and friends simply ignore NaNs as well.
All depends on what you're objectives are as to which is the more convenient construct.
7 个评论
Carlos Batista
2014-7-11
Carlos Batista
2014-7-11
dpb
2014-7-11
No, Matlab normally does NOT ignore NaN with the exception of a few cases as previously noted. The Statistics Toolbox specifically has various functions that parallel the standard ones with the naming convention nanXXX that are coded to ignore NaN. Or, as also noted, plotting routines in general cleanly ignore them. Other than that, they're treated as any other value and will propagate thru the computations and likely sprinkle more around. So, it's a very specific solution for a very specific set of circumstances, NOT a general panacea. Again, only you can judge what's the most appropriate for you case--you've not given sufficient detail for us to know.
Carlos Batista
2014-7-11
dpb
2014-7-11
Well, as before, you can use logical addressing to return the locations...
result=yourfunction(x(isfinite(x));
Now, the problem may be that this will return a linear index rather than the 3D since there isn't any regular array necessarily that the resulting points fall into and certainly not the original 3D array.
I can't envision precisely what the data would look like as described so not sure I've got any better answer.
Carlos Batista
2014-7-13
dpb
2014-7-13
Again, depends on what you mean by "sum" for the case...if you mean "ignore locations where value is NaN" then as noted before, if you have the Statistics Toolbox there are specialized funtions --
>> a=[1,2,3,NaN,5]; b=[1,2,3,8,5];
>> c=nansum([a;b])
c =
2 4 6 8 10
If you don't have the Toolbox, you've got to write special code to do the same thing. Sometimes you can be "tricky" -- for addition,
>> a(isnan(a))=0;
>> c=a+b
c =
2 4 6 8 10
>>
For multiplication, the magic value is '1'. In general you must basically process on an element by element basis taking action as desired when find the NaN. Often an anonymous function and arrayfun and/or accumarray can help immeasurably.
But, there is no one general answer.
类别
在 帮助中心 和 File Exchange 中查找有关 Marine and Underwater Vehicles 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!