Indexing with parentheses '()' must appear as the last operation of a valid indexing expression
조회 수: 20 (최근 30일)
이전 댓글 표시
des=[0.1 0.5 0.5];
L = length(des);
x = zeros(1,L)
etaa= 0.04;
w=zeros(1,L);
% w2=zeros(1,1);
e=zeros(1,1);
for k = 1:1500
x = [randi(50)/50, x(1:end-1)]
x = transpose(x);
y_fix = des * x;
y_adapt = w*x;
%%%%% error update%%%%
e(k)=y_fix-y_adapt;
%%%%% weights update%%%
for j= 1:L
w(j)(k+1)=w(j)(k)+etaa * x(j)* e(k); %MARKED
end
end
댓글 수: 0
채택된 답변
Walter Roberson
2021년 5월 31일
You declared
w=zeros(1,L);
Indexing that with a single subscript would lead you to a single double precision location.
What is your expectation, then, of what
w(j)(k)
might mean? The k'th location inside the single double precision number?
If you are creating two dimensional arrays, then you should initialize w as two dimensional rather than as a vector, and you would use two subscripts, such as w(j,k)
댓글 수: 7
Walter Roberson
2021년 6월 2일
x = zeros(1,L)
x starts out as a row vector.
x = [randi(50)/50, x(1:end-1)]
in MATLAB, when you use a single vector to index into a vector, then the result of the indexing does not depend upon the shape of the vector you indexed with, and instead depends upon the shape of the vector being indexed. So because x is a row vector, the result of x(1:end-1) is a row vector. [] between a scalar (which has one row) and a row vector (which has one row) works fine, giving you a row vector.
x = transpose(x);
You make the row vector into a column vector.
Next iteration, you encounter
x = [randi(50)/50, x(1:end-1)]
in MATLAB, when you use a single vector to index into a vector, then the result of the indexing does not depend upon the shape of the vector you indexed with, and instead depends upon the shape of the vector being indexed. So because x is now a column vector, the result of x(1:end-1) is a column vector. [] between a scalar (which has one row) and a column vector (which has multiple rows) fails.
You need to decide whether x is a row vector or a column vector, and make the code consistent for it. Do not keep changing its orientation.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!