Thread Subject: for loops within statements ?

Subject: for loops within statements ?

From: Juliette Salexa

Date: 8 Jul, 2009 20:03:03

Message: 1 of 6

I have a function called bargraph(input1,input2,...input25)

and a 6x25 matrix called A... and I want each column to be an input vector for above funtion.

In maple we can do things kind of like: bargraph(for i from 1 to 25, A(i,:)) to fill in the 25 input entries with the columns of A. Is this possible in matlab ??

If not, is there at least a neat way of doing this ??

Subject: for loops within statements ?

From: Bruno Luong

Date: 8 Jul, 2009 20:19:02

Message: 2 of 6

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <h32u1n$ons$1@fred.mathworks.com>...
> I have a function called bargraph(input1,input2,...input25)
>
> and a 6x25 matrix called A... and I want each column to be an input vector for above funtion.
>
> In maple we can do things kind of like: bargraph(for i from 1 to 25, A(i,:)) to fill in the 25 input entries with the columns of A. Is this possible in matlab ??

There must be a confusion, A(i,:) is usually called ROWS, and A(:,j) is COLUMNS.

Beside that, yes.

A = rand(25);
c = num2cell(A,1) % Split to columns - according to my definition
bargraph(c{:});

% Bruno

Subject: for loops within statements ?

From: someone

Date: 8 Jul, 2009 20:23:01

Message: 3 of 6

"Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <h32u1n$ons$1@fred.mathworks.com>...
> I have a function called bargraph(input1,input2,...input25)
>
> and a 6x25 matrix called A... and I want each column to be an input vector for above funtion.
>
> In maple we can do things kind of like: bargraph(for i from 1 to 25, A(i,:)) to fill in the 25 input entries with the columns of A. Is this possible in matlab ??

% Not that I know of.

>
> If not, is there at least a neat way of doing this ??

% The only thought that comes to mind is deal:

[input1, input2, input3, ...] = deal(A(1,:), A(2,:), A(3,:), ...)

Subject: for loops within statements ?

From: Matt Fig

Date: 8 Jul, 2009 20:24:02

Message: 4 of 6

Usually in MATLAB one would not use input1, input2, input3,... inside a function. You would pass in the matrix A then use A(:,1), A(:,2), A(:,3),.... instead. Either that or use cell arrays or structures to keep the data. For example:

>>A = magic(3);
>>B = myfunc(A);

where:

function [B] = myfunc(A)
B = (A(:,1) .^ A(:,2))./A(:,3);


NOT --- NOT --- NOT --- NOT --- NOT --- NOT:


>>A = magic(3);
>>B = myfunc(A(:,1),A(:,2),A(:,3));

where:
function [B] = myfunc(input1,input2,input3)
B = (input1 .^ input2)./input3;

Subject: for loops within statements ?

From: someone

Date: 8 Jul, 2009 20:31:19

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h32uvm$qqp$1@fred.mathworks.com>...
> "Juliette Salexa" <juliette.physicist@gmail.com> wrote in message <h32u1n$ons$1@fred.mathworks.com>...
> > I have a function called bargraph(input1,input2,...input25)
> >
> > and a 6x25 matrix called A... and I want each column to be an input vector for above funtion.
> >
> > In maple we can do things kind of like: bargraph(for i from 1 to 25, A(i,:)) to fill in the 25 input entries with the columns of A. Is this possible in matlab ??
>
> There must be a confusion, A(i,:) is usually called ROWS, and A(:,j) is COLUMNS.
>
> Beside that, yes.
>
> A = rand(25);
> c = num2cell(A,1) % Split to columns - according to my definition
> bargraph(c{:});
>
> % Bruno

Nice trick, Bruno

Subject: for loops within statements ?

From: Juliette Salexa

Date: 8 Jul, 2009 20:43:02

Message: 6 of 6

Bruno's method worked FLAWLESSLY,
Thank you to everyone who answered.

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com