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);
2 次查看(过去 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!