From: Bastian on
"Andrew Stevens" <astevens(a)JUNKusgs.gov> wrote in message <hkvlbi$14j$1(a)fred.mathworks.com>...
> Hi all,
>
> 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:
>
> %generate some data
> data=cellfun(@(x)(rand(x,1)),...
> num2cell(ceil(rand(5,1)*100)),'un',0);
>
> data =
>
> [26x1 double]
> [42x1 double]
> [34x1 double]
> [30x1 double]
> [91x1 double]
>
> %now if I want to replace values greater than 0.5 in each cell I could:
>
> for i=1:length(data)
> data{i}(data{i}>0.5)=NaN;
> end
>
> Is there a way to do it using CELLFUN? My initial attempt looked something like this:
>
> data=cellfun(@(x)(x(x>0.5)=NaN),data,'un',0);
>
> but that syntax is not valid. Any ideas?
>
> Andrew


I think this should work. It is a vectorized form of your loop:
data(data > 0.5) = NaN;