Thread Subject:
How would I use lsqcurvefit in arrayfun?

Subject: How would I use lsqcurvefit in arrayfun?

From: Jason

Date: 6 Sep, 2012 17:22:07

Message: 1 of 4

I have a 3d array of data. Each [row,col] represents a measurement with the results in the 3d dimension (call it the Z dimension).

I want to perform a lsqcurvefit on each [row,col,:] (1 x 1 x Z). I'm currently doing the following
for ii = 1:nRows
    for jj = 1:nCols
    TC(ii,jj) = lsqcurvefit(@model,1,Xdata,squeeze(Data(ii,jj,:))',0,10,Options);
    end
end

I'm curious if it's possible to use arrayfun to perform this operation, but I can't figure out how to do that. I thought something like
arrayfun(lsqcurvefit,@model,1,Xdata,Data,0,10,Options);
would be the answer, but I keep getting errors and I'm not familiar enough with arrayfun to know the proper syntax.

Thoughts?
Thanks,
Jason

Subject: How would I use lsqcurvefit in arrayfun?

From: John D'Errico

Date: 6 Sep, 2012 21:01:08

Message: 2 of 4

"Jason" wrote in message <k2am3v$5uj$1@newscl01ah.mathworks.com>...
> I have a 3d array of data. Each [row,col] represents a measurement with the results in the 3d dimension (call it the Z dimension).
>
> I want to perform a lsqcurvefit on each [row,col,:] (1 x 1 x Z). I'm currently doing the following
> for ii = 1:nRows
> for jj = 1:nCols
> TC(ii,jj) = lsqcurvefit(@model,1,Xdata,squeeze(Data(ii,jj,:))',0,10,Options);
> end
> end
>
> I'm curious if it's possible to use arrayfun to perform this operation, but I can't figure out how to do that. I thought something like
> arrayfun(lsqcurvefit,@model,1,Xdata,Data,0,10,Options);
> would be the answer, but I keep getting errors and I'm not familiar enough with arrayfun to know the proper syntax.
>
> Thoughts?
> Thanks,
> Jason

Practically speaking, its not worth the effort.

Any speed gain would be far less than the time you
would spend to write it. All it could save is some
minor amount of loop overhead in MATLAB, so not
worth doing.

John

Subject: How would I use lsqcurvefit in arrayfun?

From: Jason

Date: 7 Sep, 2012 12:21:07

Message: 3 of 4

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <k2b2uk$qdd$1@newscl01ah.mathworks.com>...
> "Jason" wrote in message <k2am3v$5uj$1@newscl01ah.mathworks.com>...
> > I have a 3d array of data. Each [row,col] represents a measurement with the results in the 3d dimension (call it the Z dimension).
> >
> > I want to perform a lsqcurvefit on each [row,col,:] (1 x 1 x Z). I'm currently doing the following
> > for ii = 1:nRows
> > for jj = 1:nCols
> > TC(ii,jj) = lsqcurvefit(@model,1,Xdata,squeeze(Data(ii,jj,:))',0,10,Options);
> > end
> > end
> >
> > I'm curious if it's possible to use arrayfun to perform this operation, but I can't figure out how to do that. I thought something like
> > arrayfun(lsqcurvefit,@model,1,Xdata,Data,0,10,Options);
> > would be the answer, but I keep getting errors and I'm not familiar enough with arrayfun to know the proper syntax.
> >
> > Thoughts?
> > Thanks,
> > Jason
>
> Practically speaking, its not worth the effort.
>
> Any speed gain would be far less than the time you
> would spend to write it. All it could save is some
> minor amount of loop overhead in MATLAB, so not
> worth doing.
>
> John


Hi John,
Thanks for the response. I understand as a speed issue, it's probably not worth the effort, but I'm more curious to know "how" to do it using arrayfun rather than the benefits of it. I figure once I get a handle on arrayfun with something like lsqcurvefit, then I'll be able to figure out the easier examples as well.

So, I'd appreciate it if you (or someone else) could show me how to do that, I'd be very greatful.

Thanks!
Jason

Subject: How would I use lsqcurvefit in arrayfun?

From: Matt J

Date: 8 Sep, 2012 13:54:07

Message: 4 of 4

"Jason" wrote in message <k2am3v$5uj$1@newscl01ah.mathworks.com>...
> I have a 3d array of data. Each [row,col] represents a measurement with the results in the 3d dimension (call it the Z dimension).
>
> I want to perform a lsqcurvefit on each [row,col,:] (1 x 1 x Z). I'm currently doing the following
> for ii = 1:nRows
> for jj = 1:nCols
> TC(ii,jj) = lsqcurvefit(@model,1,Xdata,squeeze(Data(ii,jj,:))',0,10,Options);
> end
> end
==================

What you say you're doing above doesn't seem possible. You're assigning the output of lsqcurvefit, a vector, to the scalar location TC(ii,jj). The code should be giving you errors. Nevertheless, below is an approach using ARRAYFUN,


[ii,jj]=ndgrid(1:nRows,1:nCols);
p=numel(ii);

fun=@(k) lsqcurvefit(@model,1,Xdata,squeeze(Data(ii(k),jj(k),:))',0,10,Options);

Tcell=arrayfun(fun,1:p,'UniformOutput',0);
Tcell=reshape(Tcell,size(ii));

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
arrayfun lsqcurevi... Jason 6 Sep, 2012 13:24:09
rssFeed for this Thread

Contact us