what is wrong with the following code, i get the following massege :Subscripted assignment dimension mismatch. Error in Untitled3 (line 14) y2(i)=purelin(y22);
조회 수: 3 (최근 30일)
이전 댓글 표시
clc; clear all;
a=[1 2 3; 20 21 22]
b=[1 2 3; 4 5 6; 7 8 9]
w1=[4 1 5;2 5 0;6 7 10]
w2=[10 11 12; 30 1 0]
b1=[0.4; 0.2; 0.7]
b2=[0.005; 0.01]
L=length(a)
for i=1:L
x=b(:,i);
y1=w1*x+b1;
y1=tansig(y1);
y22=w2*y1+b2;
y2(i)=purelin(y22);
end
댓글 수: 0
답변 (6개)
dpb
2018년 12월 31일
...
x=b(:,i);
y1=w1*x+b1;
y1=tansig(y1);
y22=w2*y1+b2;
where
b --> 3x1
b1 --> 3x1
b2 --> 2x1
w1 --> 3x3
w2 --> 2x3
y1 --> 3x3 X 3x1 --> 3x1
y22--> 2x3 X 3x1 --> 2x1
ergo
purelin(y22) --> 2x1
and you can't put two things into one index locations.
댓글 수: 0
Mary Abdu
2018년 12월 31일
댓글 수: 1
Walter Roberson
2018년 12월 31일
Without our seeing that other code it is difficult for us to give you a meaningful solution.
Walter Roberson
2019년 1월 1일
편집: Walter Roberson
2019년 1월 1일
We would need to know the size of w, and the size and type of inputs and outputs. It is not clear why you multiply w by 1 ?
Assuming your input w is a row vector, then:
Your w1 could be constructed as
w1 = reshape(w(1:52*N), N, []);
(Yes, it is that simple.)
Now for
y2(1,i)=purelin(w2(1,:)*(tansig(w1*in(1,i)+b1))+b2(1,:));
- w2 is constructed as 2 x N
- w1 is N x 52
- b1 is N x 1 because it is constructed from the transpose of 1 x N
- b2 is 2 x N so b2(1,:) is 1 x N
So (tansig(w1*in(1,i)+b1)) is N x 52 + N x 1. That would be an error up to R2016a, but in R2016b became well defined as giving N x 52.
w2(2,:) would be 1 x N. With the tansig returning N x 52, that would give (1 x N) * (N * 52), which is valid and gives 1 x 52. Then purelin() applied to 1 x 52 would give 1 x 52.
So the right hand side is 1 x 52, and you are trying to store that into a 1 x 1 storage location.
댓글 수: 2
Walter Roberson
2019년 1월 5일
If you need the answer to be a scalar then you will need to talk to us about how you want the formulas changed. Otherwise, use techniques such as
y2(1,i,:)=purelin(w2(1,:)*(tansig(w1*in(1,i)+b1))+b2(1,:));
madhan ravi
2019년 1월 1일
clc; clear all;
a=[1 2 3; 20 21 22]
b=[1 2 3; 4 5 6; 7 8 9]
w1=[4 1 5;2 5 0;6 7 10]
w2=[10 11 12; 30 1 0]
b1=[0.4; 0.2; 0.7]
b2=[0.005; 0.01]
L=length(a);
y2=cell(1,L); % preallocate as cell
for i=1:L
x=b(:,i);
y1=w1*x+b1;
y1=tansig(y1);
y22=w2*y1+b2;
y2{i}=purelin(y22);
end
celldisp(y2)
[y2{:}] % double array
댓글 수: 3
madhan ravi
2019년 1월 4일
편집: madhan ravi
2019년 1월 4일
you help us to help you , "still not working with me" - is completely useless , what error messgae did you get ?
Upload t and y as .mat file
Mary Abdu
2019년 1월 1일
댓글 수: 1
madhan ravi
2019년 1월 1일
편집: madhan ravi
2019년 1월 1일
See the comment in my answer , please make a comment on the answer instead of adding answers , thank you for understanding.
참고 항목
카테고리
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!