-7

I have an array of size 9 b and i am attempting at assigning values into it.

r=a(7)+u*(b(8))+v*(b(9))
b(7)=r

I get that r is indeed a 1X1 value but yet for b(7)=r i still get:

Error in ==> myFunction at 37
    b(7)=r

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

The following is the code of myFunction (the for loop with % is what i would have liked to work)

function b=myFunction(u,v,a)
    b(8)=0;
    b(9)=0
    r=a(7)+u*(b(8))+v*(b(9));
    size(a)
    size(b)
    size(r)
    b(7)=r
    r=a(6)+u*b(6+1)+v*b(6+2);
    b(6)=r;
    b(5)=a(5)+u*b(5+1)+v*b(5+2);
    b(4)=a(4)+u*b(4+1)+v*b(4+2);
    b(3)=a(3)+u*b(3+1)+v*b(3+2);
    b(2)=a(2)+u*b(2+1)+v*b(2+2);
    b(1)=a(1)+u*b(1+1)+v*b(1+2);
    %for i=7:-1:1
    %    b(i)=a(i)+u*b(i+1)+v*b(i+2);
    %end
end
SrSbSd
  • 219
  • 2
  • 4
  • 11
  • 2
    can you please print size(a) and size(b) and size(r)? – carlosdc Dec 12 '12 at 15:51
  • How? I did `display(sprintf(' sizes: %d %d %d',size(a),size(b),size(r)));` and did not get anything that would help? – SrSbSd Dec 12 '12 at 15:58
  • i got `ans = 1 7 ans = 1 9 ans =1 1` for a b and r respectivaly. Does this mean that instead of `b(7)` i need `b(1,7)`? – SrSbSd Dec 12 '12 at 16:04
  • 2
    there isn't a problem there. Which makes me suspect that you haven't posted the real sizes of a,b and r. can you post the code of myFunction? – carlosdc Dec 12 '12 at 16:08
  • Just did, the commented for loop is what i am trying to make work. The code actually writted is the same just without the use of a for loop. – SrSbSd Dec 12 '12 at 16:12

2 Answers2

1

I can not reproduce your problem. The code below works for me. Are you calling myFunction() with scalar u and v?

function myTestFunction()

a = 1:7;
u = 3;
v = 4;
b = myFunction(u,v,a)

end

function b=myFunction(u,v,a)
    b(8)=0;
    b(9)=0
%     r=a(7)+u*(b(8))+v*(b(9));
%     size(a)
%     size(b)
%     size(r)
%     b(7)=r
%     r=a(6)+u*b(6+1)+v*b(6+2);
%     b(6)=r;
%     b(5)=a(5)+u*b(5+1)+v*b(5+2);
%     b(4)=a(4)+u*b(4+1)+v*b(4+2);
%     b(3)=a(3)+u*b(3+1)+v*b(3+2);
%     b(2)=a(2)+u*b(2+1)+v*b(2+2);
%     b(1)=a(1)+u*b(1+1)+v*b(1+2);
    for i=7:-1:1
       b(i)=a(i)+u*b(i+1)+v*b(i+2);
    end
end

with the following output

b =

     0     0     0     0     0     0     0     0     0


b =

  Columns 1 through 8

       29128        7281        1821         454         114          27           7           0

  Column 9

           0
user1884905
  • 3,137
  • 1
  • 22
  • 22
  • i defined `a=[2 -5 7 -10 7 -5 2];` (changing it to a=[0 0 2 -5 7 -10 7 -5 2]; did not matter). What now then? – SrSbSd Dec 12 '12 at 16:25
  • @user44874 Ah, I missed the fact that `a` was only seven values. Changed that now. I can still run it. Can you run this code with your values of a,u and v? – user1884905 Dec 12 '12 at 16:29
  • turns out is was something with the specific libraries and sitting used on the specific computer I was on. Thank you all very much – SrSbSd Dec 17 '12 at 15:32
0

That error means you are trying to save a vector or matrix into a space that is not the correct size.

When you type a(1) = X; X must be a scalar or single value. The sizes you are reporting in the comments on this question suggest that you're trying to assign a vector to a scalar. You need to do something like b(7) = r(a_single_index)

slayton
  • 20,123
  • 10
  • 60
  • 89
  • the OP claims that size(r) is [1 1] – carlosdc Dec 12 '12 at 16:15
  • I am aware of this being the problem. I just have no idea why `r` is concidered a scalar. I defined `r=a(7)+u*(b(8))+v*(b(9));` how can i make sure it is a scalar? Thanks – SrSbSd Dec 12 '12 at 16:17