From: Oleg Komarov on 17 Feb 2010 15:03 "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 cellfun doesn't support explicit assignment (=). cellfun(@(x) accumarray(find(x > 0.5),x(x > 0.5),[],[],NaN) , data,'un',0); This one works only if any values of ALL cell meets the condition > 0.5. If somebody finds another way...else you can convert to double with cell2mat and riconvert back to cell. Oleg
|
Pages: 1 Prev: Reposted Thread on Sorting Vectors Next: How difficult would it be to make a UI like this? |