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:
about loops

Subject: about loops

From: Oscar

Date: 8 Nov, 2010 16:28:05

Message: 1 of 5

Hi, I have a problem with vectors and loops

Z=[z1;z2;z3;O]

where z1,z2,z3 and O are individual 201x1 matrices
our teacher told us to use loops to define Z, here's the code that's apparently wrong, and i'd appreciate it if you could help me with it:

for n=1:201;
    Z(n)=[z1(n,1) z2(n,1) z3(n,1) O(n,1)];
end

When i run it it says:

??? In an assignment A(I) = B, the number of elements in B and
 I must be the same.

What am I doing wrong?
Thanks

Subject: about loops

From: Sean

Date: 8 Nov, 2010 16:40:07

Message: 2 of 5

"Oscar " <obylund@yahoo.se> wrote in message <ib98el$q21$1@fred.mathworks.com>...
> Hi, I have a problem with vectors and loops
>
> Z=[z1;z2;z3;O]
>
> where z1,z2,z3 and O are individual 201x1 matrices
> our teacher told us to use loops to define Z, here's the code that's apparently wrong, and i'd appreciate it if you could help me with it:
>
> for n=1:201;
> Z(n)=[z1(n,1) z2(n,1) z3(n,1) O(n,1)];
> end
>
> When i run it it says:
>
> ??? In an assignment A(I) = B, the number of elements in B and
> I must be the same.
>
> What am I doing wrong?
> Thanks

you're trying to fit a 1x4 vector ([z1(n,1) z2(n,1) z3(n,1) O(n,1)]) in a 1x1 spot (Z(n))

Try this:
Z = zeros(length(z1),4); %preallocate
for n = 1:length(Z)
  Z(N,:) = [z1(n,1) z2(n,1) z3(n,1) O(n,1)]; %all 4 columns in the nth row
end

Subject: about loops

From: Sean

Date: 8 Nov, 2010 16:46:04

Message: 3 of 5


> you're trying to fit a 1x4 vector ([z1(n,1) z2(n,1) z3(n,1) O(n,1)]) in a 1x1 spot (Z(n))
>
> Try this:
> Z = zeros(length(z1),4); %preallocate
> for n = 1:length(Z)
> Z(N,:) = [z1(n,1) z2(n,1) z3(n,1) O(n,1)]; %all 4 columns in the nth row
> end

Typo: should be lowercase n
Z(n,:) = [z1(n,1) z2(n,1) z3(n,1) O(n,1)]; %all 4 columns in the nth row

Subject: about loops

From: Oscar

Date: 8 Nov, 2010 16:56:03

Message: 4 of 5

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ib99gc$5v0$1@fred.mathworks.com>...
>
> > you're trying to fit a 1x4 vector ([z1(n,1) z2(n,1) z3(n,1) O(n,1)]) in a 1x1 spot (Z(n))
> >
> > Try this:
> > Z = zeros(length(z1),4); %preallocate
> > for n = 1:length(Z)
> > Z(N,:) = [z1(n,1) z2(n,1) z3(n,1) O(n,1)]; %all 4 columns in the nth row
> > end
>
> Typo: should be lowercase n
> Z(n,:) = [z1(n,1) z2(n,1) z3(n,1) O(n,1)]; %all 4 columns in the nth row


Look at that, it worked, thanks a lot guys

Subject: about loops

From: dpb

Date: 8 Nov, 2010 17:02:31

Message: 5 of 5

Oscar wrote:
> Hi, I have a problem with vectors and loops
>
> Z=[z1;z2;z3;O]
>
> where z1,z2,z3 and O are individual 201x1 matrices
> our teacher told us to use loops to define Z, here's the code that's
> apparently wrong, and i'd appreciate it if you could help me with it:
>
> for n=1:201;
> Z(n)=[z1(n,1) z2(n,1) z3(n,1) O(n,1)];
> end
>
> When i run it it says:
>
> ??? In an assignment A(I) = B, the number of elements in B and
> I must be the same.
>
> What am I doing wrong?

The short answer is that Z(n) is a single element of Z and
[z1(n,1) z2(n,1) z3(n,1) O(n,1)] is a row vector of length four. Ergo,
you're trying to put four values into one memory location.

Somewhat longer -- it's not clear what the intent is --

[z1;z2;z3;O]

is the vertical concatenation of the four arrays that would lead to a
4*201x1 column vector--the ";" does that. What your loop would do
(assuming the syntax were correct) would build a 201x4 array. Which is
wanted for sure?

Assuming it really is [z1 z2 z3 O] wanted instead, first you'll want to
preallocate the Z array (doc zeros % hint :) ) and then how would you
refer to the full n-th row of Z instead of a single element?

--

Tags for 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