From: Nathan on
On Nov 23, 12:26 pm, "Elvin D'Souza" <elvin.dso...(a)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
From: Nathan on
On Nov 23, 12:44 pm, "Elvin D'Souza" <elvin.dso...(a)yahoo.ca> wrote:
> "Elvin D'Souza" <elvin.dso...(a)yahoo.ca> wrote in message <heer4v$60...(a)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