From: Juliette Salexa on
Thank you Matt and Roger,

I will use this alternative instead of the matrix2num that I got on FEX,

But if you didn't know what matrix2num does ... if I replaced it by an arbitrary function,
is there a way to avoid the forloop there ?

Of course b(1:1000)=arbitraryFunction(1:1000,:)
does not work!

Thank you.
From: Walter Roberson on
Juliette Salexa wrote:

> But if you didn't know what matrix2num does ... if I replaced it by an
> arbitrary function, is there a way to avoid the forloop there ?
>
> Of course b(1:1000)=arbitraryFunction(1:1000,:)
> does not work!

No, arbitraryFunction would have to be written to be able to accept full
matrices as inputs and produce an output for each.

Unless, that is, you only care about compactness of coding and not pure
efficiency, in which case you may wish to use

b = arrayfun(@(i) arbitraryFunction(A(i,:)), 1:size(A,1));

The 'for' loop is still there; it is just disguised, probably in a
less-efficient form.

From: Juliette Salexa on
Gotchya!
So I guess this is one of those cases where the forloop is the best way (that is, if the arbitrary function cannot be vectorized like Matt and Roger did to mat2num above).

Thanks!
From: Loren Shure on
In article <hqqnit$mk2$1(a)fred.mathworks.com>,
juliette.physicist(a)gmail.com says...
> Gotchya!
> So I guess this is one of those cases where the forloop is the best way (that is, if the arbitrary function cannot be vectorized like Matt and Roger did to mat2num above).
>
> Thanks!
>

if the function can be arbitrary, then you probably have no choice but
to use a for loop at least sometimes. You could, within try-catch, try
a vectorized call, and only bail out to a for loop if that didn't work.
If you DO use a for loop, you are far better off preallocating the
output array b first rather than growing it in the loop.

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ