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:
For Loop

Subject: For Loop

From: Barry

Date: 1 Sep, 2010 17:21:19

Message: 1 of 7

Hello
I need help with simple for loop. I have 17 different matrices size [1x365] called a2,a3,a4....a18 made up of integers. What i want is to combine them into a 17x365 using a for loop.

It will fill matix d with a1 if i just put in a1 but is there any way of using for loop as a subscript to a ie a(zzz) would give a2,a3,a4,a5, etc.


d=zeros(17,365);
for z=1:17
for zz=1:365
for zzz=2:18
d(z,zz)=(a(zzz));
end
end
end

Subject: For Loop

From: ImageAnalyst

Date: 1 Sep, 2010 17:27:13

Message: 2 of 7

d(1,:) = a2;
d(2,:) = a3;
and so on down to
d(17,:) = a18;
Why make it get any more complicated that that?

Subject: For Loop

From: Steven_Lord

Date: 1 Sep, 2010 17:45:35

Message: 3 of 7



"Barry " <barryoconnell2@gmail.com> wrote in message
news:i5m22f$3j2$1@fred.mathworks.com...
> Hello
> I need help with simple for loop. I have 17 different matrices size
> [1x365] called a2,a3,a4....a18 made up of integers. What i want is to
> combine them into a 17x365 using a for loop.

If you have control over the code that's creating the variables a2, a3, etc.
then change it so it doesn't do that. See question 4.6 in the newsgroup
FAQ.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: For Loop

From: Barry

Date: 1 Sep, 2010 17:50:20

Message: 4 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <bdeeb353-22f9-42c8-a493-3fe1b0eafca4@f42g2000yqn.googlegroups.com>...
> d(1,:) = a2;
> d(2,:) = a3;
> and so on down to
> d(17,:) = a18;
> Why make it get any more complicated that that?

Well that is fine for this example,
But i have another problem similar to this and i was just wondering can you use a for loop to call a nuber as part of a variable. ie

for
z=1:10
to call variable a1,a2......a10

Subject: For Loop

From: ImageAnalyst

Date: 1 Sep, 2010 18:47:27

Message: 5 of 7

You should check out the FAQ like Steve said. But if you were stuck
with your situation, what *I'd* probably do was to have a function. A
function can take a variable number of inputs. You can determine the
number of input arguments, and these arguments are associated with an
index. Then you could keep concatenating until you reached the last
index, then return the final version of your array out of the function
back to your main calling routine.

Subject: For Loop

From: Saurabh Mahapatra

Date: 1 Sep, 2010 20:32:08

Message: 6 of 7

Just playing around but worth a look I think:

a2=rand(2,3);
a3=rand(2,3);
a4=rand(2,3);
a5=rand(2,3);

z=zeros(2, 3, 4)

for i=2:5
  eval(['z(:,:,' num2str(i-1) ')= a' num2str(i)]);
end

Subject: For Loop

From: Oleg Komarov

Date: 1 Sep, 2010 21:10:20

Message: 7 of 7

"Saurabh Mahapatra" <saurabh.mahapatra@mathworks.com> wrote in message <i5md88$d14$1@fred.mathworks.com>...
> Just playing around but worth a look I think:
>
> a2=rand(2,3);
> a3=rand(2,3);
> a4=rand(2,3);
> a5=rand(2,3);
>
> z=zeros(2, 3, 4)
>
> for i=2:5
> eval(['z(:,:,' num2str(i-1) ')= a' num2str(i)]);
> end

This is EXACTLY what every matlab user with enough experience would avoid, especially after Steven Lord pointed out the 4.6 of the MATLAB FAQ!

Use structures with dynamic fields!

Oleg

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