Indexing cannot yield multiple results in MATLAB

954 views Asked by At

I'm trying to build confusion matrix in Matlab but I'm getting this error.

    Indexing cannot yield multiple results.

Error in Untitled3 (line 184)
 [Max, argmax1]= max(simoutelem);

I want to find maximum value in this matrix and its index.I know from enter link description here that max can be used to find the value and index of the largest element. This is my code. 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

I also tested

max(simoutelem(:));

In the link I mentioned is an example of what I'm trying to implement: Create a matrix A and compute the largest elements in each column, as well as the row indices of A in which they appear.

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

My whole code:

    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
1

There are 1 answers

4
nkjt On BEST ANSWER

This error tells me that you've defined a variable max somewhere in your code.

Indexing cannot yield multiple results

Why? Because otherwise [Max, argmax1]= max(simoutelem); wouldn't be taken as a case of "Indexing".

Easy proof at command line:

[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