Thread Subject: Calling a function by looping through a vector

Subject: Calling a function by looping through a vector

From: Kirk Hammett

Date: 27 Sep, 2009 04:23:00

Message: 1 of 4

Hello everybody,
I'm trying to call a function that takes a vector and does performs an operation on it.
I have multiple vectors to send it so I though of sending the vectors in a loop:

a, b and c are all vectors.

x = ['a' 'b' 'c'];

for i = 1 : length(x)
    PerformOperation(x(i))
end

instead of:

PerformOperation(a)
PerformOperation(b)
PerformOperation(c)

Could somebody tell me how to call my function by looping through a vector? The second solution seems like a hack and I'm trying to learn the best possible way to do this.

Much appreciated.

Subject: Calling a function by looping through a vector

From: dpb

Date: 27 Sep, 2009 04:32:50

Message: 2 of 4

Kirk Hammett wrote:
> Hello everybody,
> I'm trying to call a function that takes a vector and does performs an operation on it.
> I have multiple vectors to send it so I though of sending the vectors in a loop:
>
> a, b and c are all vectors.
>
> x = ['a' 'b' 'c'];
>
> for i = 1 : length(x)
> PerformOperation(x(i))
> end
>
> instead of:
>
> PerformOperation(a)
> PerformOperation(b)
> PerformOperation(c)
>
> Could somebody tell me how to call my function by looping through
> a vector? The second solution seems like a hack and I'm trying to
> learn the best possible way to do this.

x = {a b c};
for i = 1: length(x)
   yourfunc(x{i});
end

--

Subject: Calling a function by looping through a vector

From: Kirk Hammett

Date: 27 Sep, 2009 04:58:03

Message: 3 of 4

Thank you for your help!

Subject: Calling a function by looping through a vector

From: Loren Shure

Date: 28 Sep, 2009 12:38:29

Message: 4 of 4

In article <h9mpb4$b0r$1@fred.mathworks.com>, leprekhan@yahoo.com
says...
> Hello everybody,
> I'm trying to call a function that takes a vector and does performs an operation on it.
> I have multiple vectors to send it so I though of sending the vectors in a loop:
>
> a, b and c are all vectors.
>
> x = ['a' 'b' 'c'];
>
> for i = 1 : length(x)
> PerformOperation(x(i))
> end
>
> instead of:
>
> PerformOperation(a)
> PerformOperation(b)
> PerformOperation(c)
>
> Could somebody tell me how to call my function by looping through a vector? The second solution seems like a hack and I'm trying to learn the best possible way to do this.
>
> Much appreciated.
>

The function arrayfun might help.

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

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
for loop Sprinceana 27 Sep, 2009 09:43:38
for Sprinceana 27 Sep, 2009 09:43:38
vector Sprinceana 27 Sep, 2009 09:43:31
loop Sprinceana 27 Sep, 2009 09:43:31
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com