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

2 views (last 30 days)
i am getting this error at : a=5; b=10; a1=unifrnd(a,b,4,4); x=a1*double(s3); W=unifrnd(a,b,4,4); U=W*x; alpha=4; g1=alpha*(power((U/3),(alpha-1))); I=[1 0 0 0; 0 1 0 0; 0 0 1 0 ; 0 0 0 1] for i=1:4 W(i+1)=W(i)+0.2*((I- (g1*U'))*W(i)); // here i am geeting error. end

Answers (1)

Stephen23
Stephen23 on 10 Feb 2015
Edited: Stephen23 on 10 Feb 2015
You are trying to fit a matrix into a scalar position. Have look at the line
W(i+1)=W(i)+0.2*((I- (g1*U'))*W(i))
You defined I as a matrix, so the operation
  • I-(...) is also a matrix, as is
  • W(i)+0.2*((I- (g1*U'))*W(i))
Try highlighting those bits of code and then pressing the f9 button on your keyboard: this evaluates the highlighted code. What size is I-(...) ?
On this line you try to assign this matrix to one element of the array W , with the code W(i+1). This is always an error in MATLAB, as you can only fit one element (i.e. a scalar) into one element of an array. If you want to assign multiple values, you need to learn about indexing in MATLAB.
Note also that you should avoid using i (and j) as loop variable names, as these are both names of the inbuilt imaginary unit .

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!