Thread Subject: Error :"In an assignment A(I) = B, the number of elements in B and I must be the same."

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

From: Elvin D'Souza

Date: 23 Nov, 2009 20:26:07

Message: 1 of 6

h=0.01;
s(1)= -1
k=1
while (k~=201)
    B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
    s(k+1)=s(k)+h
end

tats my code . I get an error at B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]. It says
"In an assignment A(I) = B, the number of elements in B and
 I must be the same."

If I enter a number instead of s(k), it works.
How do I fix this?

Thanks

Subject: Error :"In an assignment A(I)

From: Nathan

Date: 23 Nov, 2009 20:35:58

Message: 2 of 6

On Nov 23, 12:26 pm, "Elvin D'Souza" <elvin.dso...@yahoo.ca> wrote:
> h=0.01;
> s(1)= -1
> k=1
> while (k~=201)
>     B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
>     s(k+1)=s(k)+h
> end
>
> tats my code . I get an error at    B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]. It says
> "In an assignment  A(I) = B, the number of elements in B and
>  I must be the same."
>
> If I enter a number instead of s(k), it works.
> How do I fix this?
>
> Thanks

You're trying to send a column vector into a single element of B.
How about using B as a cell array?
doc cell

Also: your loop will never end. You do no modifications of k to allow
it to reach 201.

-Nathan

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

From: Elvin D'Souza

Date: 23 Nov, 2009 20:44:19

Message: 3 of 6

"Elvin D'Souza" <elvin.dsouza@yahoo.ca> wrote in message <heer4v$60k$1@fred.mathworks.com>...
> h=0.01;
> s(1)= -1
> k=1
> while (k~=201)
> B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
> s(k+1)=s(k)+h
> end
>
> tats my code . I get an error at B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]. It says
> "In an assignment A(I) = B, the number of elements in B and
> I must be the same."
>
> If I enter a number instead of s(k), it works.
> How do I fix this?
>
> Thanks

Sorry, the actual code is
h=0.01;
s(1)= -1
k=1
while (k~=201)
    B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
    s(k+1)=s(k)+h
    k=k+1
end


and B(k) is supposed to be a matrix. So, how do I make this work?

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

From: someone

Date: 23 Nov, 2009 20:52:01

Message: 4 of 6

"Elvin D'Souza" <elvin.dsouza@yahoo.ca> wrote in message <heer4v$60k$1@fred.mathworks.com>...
> h=0.01;
> s(1)= -1
> k=1
> while (k~=201)
> B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
> s(k+1)=s(k)+h
> end
>
> tats my code . I get an error at B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]. It says
> "In an assignment A(I) = B, the number of elements in B and
> I must be the same."
>
> If I enter a number instead of s(k), it works.
> How do I fix this?
>
> Thanks

I see a couple of problems right away.
Do you want B to be a cell array?
If so, use B{k} instead of B(k).
(Note the curley brackets.)
The error message says you are trying to assign a 2X2 matrix
to a single element of a vector. Need a cell array to do this.

You also need to increment k before the end of your while loop.

    s(k+1)=s(k)+h
    k = k + 1; % I'm guessing that this is what you mean.
end

You should preallocate s & B before the while loop,
if you want the code to run faster, but not necessary.
Something like:

s = zeros(201,1);
s(1)= -1
k=1
while (k~=201)

Subject: Error :"In an assignment A(I)

From: Nathan

Date: 23 Nov, 2009 21:00:53

Message: 5 of 6

On Nov 23, 12:44 pm, "Elvin D'Souza" <elvin.dso...@yahoo.ca> wrote:
> "Elvin D'Souza" <elvin.dso...@yahoo.ca> wrote in message <heer4v$60...@fred.mathworks.com>...
> > h=0.01;
> > s(1)= -1
> > k=1
> > while (k~=201)
> >     B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
> >     s(k+1)=s(k)+h
> > end
>
> > tats my code . I get an error at    B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]. It says
> > "In an assignment  A(I) = B, the number of elements in B and
> >  I must be the same."
>
> > If I enter a number instead of s(k), it works.
> > How do I fix this?
>
> > Thanks
>
> Sorry, the actual code is
> h=0.01;
> s(1)= -1
> k=1
> while (k~=201)
>     B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
>     s(k+1)=s(k)+h
>     k=k+1
> end
>
> and B(k) is supposed to be a matrix. So, how do I make this work?

B(k) could be a cell of matrices.

You cannot assign a 2x2 matrix to 1 element of a regular matrix.
That's why I suggested using B as a cell array. Cell elements can
contain pretty much anything (even your 2x2 matrix).
In order to do what you are trying to do, you have to think harder.

If you insist on using B as a matrix...
B will be a (200*2)x2 matrix (200 2x2 matrices)
so initialized B = zeros(200,2)
replace your B(k) = ... line with the following 4 lines.
B(1+2*k-1,1) = 4
B(2+2*k-1,2) = -1
B(1+2*k-1,1) = 5-5*s(k)
B(2+2*k-1,2) = 2.5+5*s(k)
There you go.
I would really use cells, though. They organize these matrices so much
nicer.

-Nathan

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

From: Elvin D'Souza

Date: 23 Nov, 2009 21:30:19

Message: 6 of 6

"someone" <someone@somewhere.net> wrote in message <heeslh$c3h$1@fred.mathworks.com>...
> "Elvin D'Souza" <elvin.dsouza@yahoo.ca> wrote in message <heer4v$60k$1@fred.mathworks.com>...
> > h=0.01;
> > s(1)= -1
> > k=1
> > while (k~=201)
> > B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]
> > s(k+1)=s(k)+h
> > end
> >
> > tats my code . I get an error at B(k)=[4 (5-5*s(k)); (2.5+5*s(k)) -1]. It says
> > "In an assignment A(I) = B, the number of elements in B and
> > I must be the same."
> >
> > If I enter a number instead of s(k), it works.
> > How do I fix this?
> >
> > Thanks
>
> I see a couple of problems right away.
> Do you want B to be a cell array?
> If so, use B{k} instead of B(k).
> (Note the curley brackets.)
> The error message says you are trying to assign a 2X2 matrix
> to a single element of a vector. Need a cell array to do this.
>
> You also need to increment k before the end of your while loop.
>
> s(k+1)=s(k)+h
> k = k + 1; % I'm guessing that this is what you mean.
> end
>
> You should preallocate s & B before the while loop,
> if you want the code to run faster, but not necessary.
> Something like:
>
> s = zeros(201,1);
> s(1)= -1
> k=1
> while (k~=201)

Thanks, that fixed it. But now I am having a plotting problem, could you take a look at it. It is in another post.

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