Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
avoid forloop

Subject: avoid forloop

From: Juliette Salexa

Date: 22 Apr, 2010 22:13:22

Message: 1 of 9

Is there a way to avoid the forloop here:

for i=1:10000000
b(i)=matrix2num(a(i,:));
end

?

Perhaps using cell arrays ?

Subject: avoid forloop

From: Matt Fig

Date: 22 Apr, 2010 22:47:21

Message: 2 of 9

And what is MATRIX2NUM? I sure don't have it...

Subject: avoid forloop

From: Roger Stafford

Date: 22 Apr, 2010 22:50:26

Message: 3 of 9

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <hqqhm2$ipj$1@fred.mathworks.com>...
> Is there a way to avoid the forloop here:
>
> for i=1:10000000
> b(i)=matrix2num(a(i,:));
> end
>
> ?
>
> Perhaps using cell arrays ?
--------------------
 n = size(a,2);
 b = a*10.^((n-1:-1:0).');

I am assuming your matrix2num is the one written by Aman Siddiqi at:
http://www.mathworks.nl/matlabcentral/fileexchange/7264
which in this case converts decimal digits to decimal numbers.

Roger Stafford

Subject: avoid forloop

From: Matt Fig

Date: 22 Apr, 2010 22:55:22

Message: 4 of 9

Oh, if that is the case:

% Data
a = magic(3)

% Engine
b = sum(a.*repmat(10.^(size(a,2)-1:-1:0),size(a,1),1),2)

Subject: avoid forloop

From: Matt Fig

Date: 22 Apr, 2010 22:57:20

Message: 5 of 9

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message
> --------------------
> n = size(a,2);
> b = a*10.^((n-1:-1:0).');
> Roger Stafford

Much nicer!

Subject: avoid forloop

From: Juliette Salexa

Date: 22 Apr, 2010 23:15:27

Message: 6 of 9

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.

Subject: avoid forloop

From: Walter Roberson

Date: 22 Apr, 2010 23:37:47

Message: 7 of 9

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.

Subject: avoid forloop

From: Juliette Salexa

Date: 22 Apr, 2010 23:54:05

Message: 8 of 9

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!

Subject: avoid forloop

From: Loren Shure

Date: 28 Apr, 2010 16:02:03

Message: 9 of 9

In article <hqqnit$mk2$1@fred.mathworks.com>,
juliette.physicist@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

Tags for this Thread

No tags are associated with this thread.

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.

Contact us