matlab中有没有对矩阵的某一行或某一列进行操作的函数?

时间:2021-04-24 15:03:40
matlab中有没有对矩阵的某一行或某一列进行单独操作的函数?
比如把某一行或某一列赋给一个向量
我需要将矩阵的某几行或列用作神经网络的输入
本人是matlab菜鸟,请各位大虾指教

8 个解决方案

#1


当然有

#2


有的,有的。找些例子看看就行,很方便。

LONG LONG TIME AGO,我用MATLAB编了BP ANN,只用了1屏的代码,而速度比当时的C/C++程序还快~~~

要不要,好象我那程序还在。

#3


楼上的,如果能找到就给我吧,多谢了~~我可能要用到BP网络的
现在急需把矩阵的某一行或某一列赋给一个向量,应该用什么指令啊
我的那本书上好像没有提到

#4


Try this:

>> x=randn(4,5);
>> v=randn(4,1);
>> x(:,2)=v;

程序我去找找。

#5


可以了,谢谢^_^
你的程序还能找到吗?

#6


function [output,weight] = ANNBP(xin,yout)
% [output,weight] = ANNBP(xin,yout)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initial Section

%xin, yout, layers, grad, moment, precision, mse
if nargin==0
xin=[0,0,0;0,0,1;0,1,0;0,1,1;1,0,0;1,0,1;1,1,0;1,1,1];
yout=[0,1,1,0,1,1,0,1]';
end

layers=[length(xin(1,:)),8,length(yout(1,:))];
grad=0.50;
moment=0.0;
precision=0.0001;
mse=1;

nsam=length(yout);
nlayer=length(layers);

xintrs=0.8*(xin-ones(nsam,1)*min(xin))./(ones(nsam,1)*max(xin)-...
ones(nsam,1)*min(xin))+0.1;

x{1}=xintrs;
y=0.8*(yout-min(yout))/(max(yout)-min(yout))+0.1;

for i=1:nlayer-1
w{i}=randn(layers(i),layers(i+1));
dw{i}=zeros(layers(i),layers(i+1));
delta{i}=zeros(nsam,layers(i+1));
x{i+1}=zeros(nsam,layers(i+1));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[x]=fp(x,w,nlayer);
rsd1=sum((x{nlayer}-y(:)).^2);
[dw]=bp(x,y,w,delta,dw,grad,moment,nlayer);
for iw=1:10
w=pluscell(w,dw);
[x]=fp(x,w,nlayer);
rsd2=sum((x{nlayer}-y(:)).^2);
disp(rsd2)
if rsd2<rsd1
grad=1.05;moment=0.8;
else
grad=0.7;moment=0.0;
end
[dw]=bp(x,y,w,delta,dw,grad,moment,nlayer);
w=pluscell(w,dw);
[x]=fp(x,w,nlayer);
end

output=x{nlayer};
output=(output-0.1)/0.8*(max(yout)-min(yout))+min(yout);
weight=w;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [x]=fp(x,w,nlayer)
function [x]=fp(x,w,nlayer)

for i=1:nlayer-1
x{i+1}=1./(1+exp(-x{i}*w{i}));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [dw]=bp(x,y,w,delta,dw,grad,moment,nlayer)
function [dw]=bp(x,y,w,delta,dw,grad,moment,nlayer)

for i=1:nlayer-1:-1:1
if i==nlayer-1
delta{i}=-(x{nlayer}-y(:)).*x{nlayer}.*(1-x{nlayer});
else
delta{i}=x{i+1}.*(1-x{i+1}).*(delta{i+1}*(w{i+1})');
end
dw{i}=grad*((x{i})'*delta{i})+moment*dw{i};
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [z]=pluscell(x,y)
function [z]=pluscell(x,y)

for i=1:length(x)
z{i}=x{i}+y{i};
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





#7


不知道这个版本对不对,我好久没用过了,你可以参考

#8


多谢jhwang2k(Ted)!

#1


当然有

#2


有的,有的。找些例子看看就行,很方便。

LONG LONG TIME AGO,我用MATLAB编了BP ANN,只用了1屏的代码,而速度比当时的C/C++程序还快~~~

要不要,好象我那程序还在。

#3


楼上的,如果能找到就给我吧,多谢了~~我可能要用到BP网络的
现在急需把矩阵的某一行或某一列赋给一个向量,应该用什么指令啊
我的那本书上好像没有提到

#4


Try this:

>> x=randn(4,5);
>> v=randn(4,1);
>> x(:,2)=v;

程序我去找找。

#5


可以了,谢谢^_^
你的程序还能找到吗?

#6


function [output,weight] = ANNBP(xin,yout)
% [output,weight] = ANNBP(xin,yout)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initial Section

%xin, yout, layers, grad, moment, precision, mse
if nargin==0
xin=[0,0,0;0,0,1;0,1,0;0,1,1;1,0,0;1,0,1;1,1,0;1,1,1];
yout=[0,1,1,0,1,1,0,1]';
end

layers=[length(xin(1,:)),8,length(yout(1,:))];
grad=0.50;
moment=0.0;
precision=0.0001;
mse=1;

nsam=length(yout);
nlayer=length(layers);

xintrs=0.8*(xin-ones(nsam,1)*min(xin))./(ones(nsam,1)*max(xin)-...
ones(nsam,1)*min(xin))+0.1;

x{1}=xintrs;
y=0.8*(yout-min(yout))/(max(yout)-min(yout))+0.1;

for i=1:nlayer-1
w{i}=randn(layers(i),layers(i+1));
dw{i}=zeros(layers(i),layers(i+1));
delta{i}=zeros(nsam,layers(i+1));
x{i+1}=zeros(nsam,layers(i+1));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[x]=fp(x,w,nlayer);
rsd1=sum((x{nlayer}-y(:)).^2);
[dw]=bp(x,y,w,delta,dw,grad,moment,nlayer);
for iw=1:10
w=pluscell(w,dw);
[x]=fp(x,w,nlayer);
rsd2=sum((x{nlayer}-y(:)).^2);
disp(rsd2)
if rsd2<rsd1
grad=1.05;moment=0.8;
else
grad=0.7;moment=0.0;
end
[dw]=bp(x,y,w,delta,dw,grad,moment,nlayer);
w=pluscell(w,dw);
[x]=fp(x,w,nlayer);
end

output=x{nlayer};
output=(output-0.1)/0.8*(max(yout)-min(yout))+min(yout);
weight=w;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [x]=fp(x,w,nlayer)
function [x]=fp(x,w,nlayer)

for i=1:nlayer-1
x{i+1}=1./(1+exp(-x{i}*w{i}));
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [dw]=bp(x,y,w,delta,dw,grad,moment,nlayer)
function [dw]=bp(x,y,w,delta,dw,grad,moment,nlayer)

for i=1:nlayer-1:-1:1
if i==nlayer-1
delta{i}=-(x{nlayer}-y(:)).*x{nlayer}.*(1-x{nlayer});
else
delta{i}=x{i+1}.*(1-x{i+1}).*(delta{i+1}*(w{i+1})');
end
dw{i}=grad*((x{i})'*delta{i})+moment*dw{i};
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% [z]=pluscell(x,y)
function [z]=pluscell(x,y)

for i=1:length(x)
z{i}=x{i}+y{i};
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





#7


不知道这个版本对不对,我好久没用过了,你可以参考

#8


多谢jhwang2k(Ted)!