From: Andrew Stevens on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hljog3$5sm$1(a)fred.mathworks.com>...
> Dear Andrew!
>
> > I am trying to figure out a way to replace values in a cell array with NaN. I can do it with a loop, but I have somehow got myself into the CELLFUN habit and don't much like the look of the loop:
>
> After some nice solutions have been found, could you be so kind and compare the execution speed with a simple loop?
>
> for iC = 1:numel(C)
> aC = C{iC};
> aC(aC >= 0.5) = NaN;
> C{iC} = aC;
> end
>
> Although you don't "like" it, it could be more efficient.
>
> Jan


Jan,
Of course you and Matt are right about the execution speed. For my application, I am not worried about speed.

In many cases for me, using cellfun over a loop is more compact, but that is not the case in this instance (Matt's loop has less characters than Walter's solution). So is there a reason to use the cellfun method for this problem (besides really confusing those you share your code with)? Not sure, but there are cases where Walters method will be slightly more compact:

%generate a structure containing cells
names={'andrew','bastian','walter',...
'oleg','nathan','yair',...
'jos','steven','jan','matt'};
data=cellfun(@(x,y)(rand(50,1)),...
num2cell(1:length(names)),'un',0)';
dstr=cell2struct(data,names);


%replace using walters method
d2=structfun(@(x)(x + (sign(inf * (x <= 0.5)) - 1)),...
dstr,'un',0);

%loop
d2=dstr;
fields=fieldnames(dstr);
for i = 1:length(fields);
d2.(fields{i})(d2.(fields{i})<=0.5) = NaN;
end

Anyway, thanks for all the input!
Andrew
From: Walter Roberson on
Jos (10584) wrote:

> Here is a somewhat simpler alternative for the function:
>
> @(x) x + 0 ./ ~(x>0.5)

That's a nice improvement, Jos!!

It leads to a simplification by one matrix operation:

@(x) x + 0 ./ (x <= 0.5)
From: Steven Lord on

"Oleg Komarov" <oleg.komarovRemove.this(a)hotmail.it> wrote in message
news:hljq9l$48t$1(a)fred.mathworks.com...
>> So for the "much-requested built-in IFF function", would you want it to
>> demonstrate this behavior or to operate element-by-element, and more
>> importantly how many people do you believe would expect it to work the
>> other way? [I'm genuinely interested in your answers to the two parts of
>> this question.]
>>
>> --
>> Steve Lord
>> slord(a)mathworks.com
>> comp.soft-sys.matlab (CSSM) FAQ:
>> http://matlabwiki.mathworks.com/MATLAB_FAQ
>
> Not sure i understod your question but considering
> cellfun(@(x) iff(x>.5,NaN,x), data);
> I would expect for ii = numel(data)
> IDX = data{1} > .5 data{1}(IDX) = NaN
> data{1}(~IDX) = x % (which is equal to itself in this specific case)
> end
>
> I wouldn't want if all(data{1} > .5) data{1}(:) = NaN; % neither data{1} =
> NaN;
> else data{1}(:) = x;
> end

Let me simplify the question by removing the extraneous CELLFUN. What would
your expectation be for the contents of y after these lines of code were
executed?

clear y
x = 0:0.1:1;
y = iff(x > 0.5, NaN, x)

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ