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

回答(6 个)

dpb
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.

Mary Abdu
Mary Abdu 2018-12-31
thank you; i have already corrected by the foloowing:
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
however, still i could not solve the same problem by the same action in my main code for another problem

Mary Abdu
Mary Abdu 2018-12-31
w=w*1;
load('inputs.mat');
load('outputs.mat');
in=inputs; % loads inputs into variable 'in'
t=outputs; % loads output1 into variable 't'
% w
N=3;
w1=[w(1:N);w(N+1:2*N);w(2*N+1:3*N);w(3*N+1:4*N);w(4*N+1:5*N);w(5*N+1:6*N);w(6*N+1:7*N);w(7*N+1:8*N);w(8*N+1:9*N);w(9*N+1:10*N);w(10*N+1:11*N);w(11*N+1:12*N);w(12*N+1:13*N);w(13*N+1:14*N);w(14*N+1:15*N);w(15*N+1:16*N);w(16*N+1:17*N);w(17*N+1:18*N);w(18*N+1:19*N);w(19*N+1:20*N);w(20*N+1:21*N);w(21*N+1:22*N);w(22*N+1:23*N);w(23*N+1:24*N);w(24*N+1:25*N);w(25*N+1:26*N);w(26*N+1:27*N);w(27*N+1:28*N);w(28*N+1:29*N);w(29*N+1:30*N);w(30*N+1:31*N);w(31*N+1:32*N);w(32*N+1:33*N);w(33*N+1:34*N);w(34*N+1:35*N);w(35*N+1:36*N);w(36*N+1:37*N);w(37*N+1:38*N);w(38*N+1:39*N);w(39*N+1:40*N);w(40*N+1:41*N);w(41*N+1:42*N);w(42*N+1:43*N);w(43*N+1:44*N);w(44*N+1:45*N);w(45*N+1:46*N);w(46*N+1:47*N);w(47*N+1:48*N);w(48*N+1:49*N);w(49*N+1:50*N);w(50*N+1:51*N);w(51*N+1:52*N)]';
b1=w(52*N+1:53*N)';
w2=[w(53*N+1:54*N); w(54*N+1:55*N)];
b2=[w((54*N+1:55*N)+1);w((54*N+1:55*N)+2)];
L=length(t)
for i=1:L
y2(1,i)=purelin(w2(1,:)*(tansig(w1*in(1,i)+b1))+b2(1,:));
y2(2,i)=purelin(w2(2,:)*(tansig(w1*in(2,i)+b1))+b2(2,:));
end
this is the code inputs dimention is 52x12000
outputs dimention is 2x12000

Walter Roberson
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.

madhan ravi
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
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
Mary Abdu 2019-1-1
w1=Nx52, b1=Nx1, b2=2x1, w2=2x3
thank you mister madhan, it is look like working with my code (below is the full code) but now i am getting the error that of Undefined operator '-' for input arguments of type
'cell'. Error in fitness_ANNx2 (line 28)
mse1=abs(sum(((t(1,:)-y2(1,:))+(t(2,:)-y2(2,:)))/2));
function mse1=fitness_ANNx2(w)
w=w*1;
load('inputs.mat');
load('outputs.mat');
in=inputs; % loads inputs into variable 'in'
t=outputs; % loads output1 into variable 't'
% w
N=3;
w1=[w(1:N);w(N+1:2*N);w(2*N+1:3*N);w(3*N+1:4*N);w(4*N+1:5*N);w(5*N+1:6*N);w(6*N+1:7*N);w(7*N+1:8*N);w(8*N+1:9*N);w(9*N+1:10*N);w(10*N+1:11*N);w(11*N+1:12*N);w(12*N+1:13*N);w(13*N+1:14*N);w(14*N+1:15*N);w(15*N+1:16*N);w(16*N+1:17*N);w(17*N+1:18*N);w(18*N+1:19*N);w(19*N+1:20*N);w(20*N+1:21*N);w(21*N+1:22*N);w(22*N+1:23*N);w(23*N+1:24*N);w(24*N+1:25*N);w(25*N+1:26*N);w(26*N+1:27*N);w(27*N+1:28*N);w(28*N+1:29*N);w(29*N+1:30*N);w(30*N+1:31*N);w(31*N+1:32*N);w(32*N+1:33*N);w(33*N+1:34*N);w(34*N+1:35*N);w(35*N+1:36*N);w(36*N+1:37*N);w(37*N+1:38*N);w(38*N+1:39*N);w(39*N+1:40*N);w(40*N+1:41*N);w(41*N+1:42*N);w(42*N+1:43*N);w(43*N+1:44*N);w(44*N+1:45*N);w(45*N+1:46*N);w(46*N+1:47*N);w(47*N+1:48*N);w(48*N+1:49*N);w(49*N+1:50*N);w(50*N+1:51*N);w(51*N+1:52*N)]';
b1=w(52*N+1:53*N)';
w2=[w(53*N+1:54*N); w(54*N+1:55*N)];
b2=[w((54*N+1:55*N)+1);w((54*N+1:55*N)+2)];
L=length(t);
y2=cell(1,L);
for i=1:L
x=in(:,i);
y1=w1*x+b1;
y1=tansig(y1);
y22=w2*y1+b2;
y2{i}=purelin(y22);
end
celldisp(y2);
[y2{:}];
mse1=abs(sum(((t(1,:)-y2(1,:))+(t(2,:)-y2(2,:)))/2));

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by