我正在尝试在Matlab中构建混淆矩阵,但是我收到了这个错误。
Indexing cannot yield multiple results.
Error in Untitled3 (line 184)
[Max, argmax1]= max(simoutelem);
我想在此矩阵及其索引中找到最大值。我从enter link description here知道max
可以用来查找最大元素的值和索引。
这是我的代码。
MATRIS =零(10,2316);
for i=1:300
simoutelem=Foutput(:,i);
[Max, argmax1]= max(simoutelem);
matris(argmax1,i)=1;
end
confusion=zeros(10,10);
for i=1:10
for j=1:26
confusion(i,1)=((matris(i,j)+confusion(i,1)))/26;
end
end
for i=1:10
for k=2:10
for j=(k-1)*26+1:(k-1)*26+30
confusion(i,k)=((matris(i,j)+confusion(i,k)))/30;
end
end
end
我也测试了
max(simoutelem(:));
在我提到的链接中,我正在尝试实现的示例: 创建一个矩阵A并计算每列中最大的元素,以及它们出现的A的行索引。
A = [1 9 -2; 8 4 -5]
A =
1 9 -2
8 4 -5
[M,I] = max(A)
M =
8 9 -2
I =
2 1 1
我的整个代码:
TrainSet=cell(200);
TestSet=cell(30);
%zeros(n,m);
%......................................................................................
%Reading train dataset
directory=dir('C:\Users\Rihanna\Desktop\TrainSet');
for i=3:length(directory)
folderstring=strcat('C:\Users\Rihanna\Desktop\TrainSet\',directory(i).name);
directory1=dir(folderstring);
for j=3:length(directory1)
TrainSet{i-2}{j-2}=audioread(strcat(folderstring,'\',directory1(j).name));
end
end
%--------------------------------------------------------------------------------
%Reading test dataset
directory2=dir('C:\Users\Rihanna\Desktop\TestSet');
for i=3:length(directory2)
folderstring=strcat('C:\Users\Rihanna\Desktop\TestSet\',directory2(i).name);
directory3=dir(folderstring);
for j=3:length(directory3)
TestSet{i-2}{j-2}=audioread(strcat(folderstring,'\',directory3(j).name));
end
end
%.......................................................................................
% make files equal in size using zero padding
%find longest
max=0;
TrainSize=0;
TestSize=0;
for i=3: length(directory)
for j=3:length(directory1)
if(size(TrainSet{i-2}{j-2},1) > TrainSize)
TrainSize=size(TrainSet{i-2}{j-2},1);
end
end
end
%----------------------------------------------------------------------------------------------
for i=3:length(directory2)
folderstring=strcat('C:\Users\Rihanna\Desktop\TestSet\',directory2(i).name);
directory3=dir(folderstring);
for j=3:length(directory3)
if(size(TestSet{i-2}{j-2},1) > TestSize)
TestSize=size(TestSet{i-2}{j-2},1);
%tempsize=size(TestSet{i-2}{j-2},1);
% TestSize=max(tempsize,TestSize);
end
end
end
if(TestSize>TrainSize)
max=TestSize;
else
max=TrainSize;
end
%zero padding-----------------------------------
for i=3: length(directory2)
folderstring=strcat('C:\Users\Rihanna\Desktop\TestSet\',directory2(i).name);
directory3=dir(folderstring);
for j=3:length(directory3)
m=zeros(1,max-size(TestSet{i-2}{j-2},1));
t=[TestSet{i-2}{j-2}',m];
TestSet{i-2}{j-2}=t';
end
end
for i=3: length(directory)
for j=3:length(directory1)
m=zeros(1,max-size(TrainSet{i-2}{j-2},1));
t=[TrainSet{i-2}{j-2}',m];
TrainSet{i-2}{j-2}=t';
end
end
%----------------------------------------------------------------------------------------
%Implementation of mlp
%newff( p,t ,[10 5], {'tansig' 'logsig'},'traingd','','mse',{},{},'');
%net = init(net);
%[trained_net, stats] = train(net, p, t);
%coeff = 0.2;
%
%frame=20ms-160sample
%y = buffer(window, 1, floor(64 * 0.5));
wnum=floor(max/80)-1;
w=hamming(160);
%for k=1: wnum
% if k==1
% sig=TestSet{i-2}{j-2};
% final(1,:)=(sig,1:160);
%end
%end
%--------------------------------------framing
% finalLpc[10][190];
%...............................................Test...............
for i=3:length(directory2)
folderstring=strcat('C:\Users\Rihanna\Desktop\TestSet\',directory(i).name);
directory3=dir(folderstring);
for j=3:length(directory3)
%cats=[];
lpcTest=[];
for k=1 :wnum
sig1=TestSet{i-2}{j-2};
if k==1
framTest=sig1(1:160);
else
framTest=sig1((k-1)*80+1:((k+1)*80));
end
winTest=framTest.* w;
cats1=lpc(winTest,12);
catss1(k,:)=cats1(2:13);
end
features1=12*(floor(max/80)-1);
%features=2316
n=length(directory3);
anninput1((i-3)*(length(directory3)-2 ) +(j-2),:) =reshape(catss1,1,2316);
end
end
%.............
anninput1=anninput1';
%...............................................Train..................
for i=3:length(directory)
folderstring=strcat('C:\Users\Rihanna\Desktop\TrainSet\',directory(i).name);
directory1=dir(folderstring);
for j=3:length(directory1)
%cats=[];
lpcTrain=[];
for k=1 :wnum
sig=TrainSet{i-2}{j-2};
if k==1
framTrain=sig(1:160);
else
framTrain=sig((k-1)*80+1:((k+1)*80));
%((k-1).*80)+1 :(k+1).*80
end
winTrain=framTrain.* w;
cats=lpc(winTrain,12);
catss(k,:)=cats(2:13);
% lpcTrain =[lpcTrain,l];
end
features=12*(floor(max/80)-1);
%features=2316
n=length(directory1);
anninput((i-3)*(length(directory1)-2 ) +(j-2),:) =reshape(catss,1,2316);
%input(i*(190)+(ii),:)=reshape(l_effective,1,2316);
% finalLpc{i-2}{j-2}=lpcTrain;
%1900*2316
end
end
%...............................ANN...............................................................
input=anninput';
output=[zeros(10,190) ones(10,190) repmat(2,10,190) repmat(3,10,190) repmat(4,10,190) repmat(5,10,190) repmat(6,10,190) repmat(7,10,190) repmat(8,10,190) repmat(9,10,190) ];
net=newff(input,output,1158);
net=init(net);
%Learning rate
net.trainParam.lr=0.2;
net.trainParam.showWindow=1;
net.trainParam.min_grad=0;
net.trainParam.mem_reduc=50;
net.efficiency.memoryReduction = 60;
net= trainrp(net, input,output);
%plot_xor(net);
%...............................................
Foutput=sim(net, anninput1); % Returns one output.
%figure, plotconfusion(Foutput, sim(net,input));
matris=zeros(10,2316);
for i=1:300
simoutelem=Foutput(:,i);
[Max, argmax1]= max(simoutelem);
matris(argmax1,i)=1;
end
confusion=zeros(10,10);
for i=1:10
for j=1:26
confusion(i,1)=((matris(i,j)+confusion(i,1)))/26;
end
end
for i=1:10
for k=2:10
for j=(k-1)*26+1:(k-1)*26+30
confusion(i,k)=((matris(i,j)+confusion(i,k)))/30;
end
end
end
答案 0 :(得分:0)
此错误告诉我您已在代码中的某处定义了变量max
。
索引不能产生多重结果
为什么呢?因为否则[Max, argmax1]= max(simoutelem);
不会被视为“索引”的情况。
在命令行轻松验证:
[a b] = max([1 2 3 4 5]) % works
max = 1:100;
[a b] = max([1 2 3 4 5]) % gives your error
clear max
[a b] = max([1 2 3 4 5]) % works again