error: Matrix dimensions must agree in matlab for building confusion matrix
error: Matrix dimensions must agree in matlab for building confusion matrix
我知道以前有人问过这个问题,但我没有从他们那里得到答案。而且我也没有从 mathworks.com 那里得到太多
我正在尝试为 ANN 构建混淆矩阵。
testSize 是我拥有的测试集文件的数量。
output = sim(net,Yt);%testing network
confusionMatrix(10,10);%building confusion matrix
for i=1:(testSize-2)*10
for j=1:10
m(j) = abs(output(i,i)-1);
end
minimum = find(m == min(m));
confusionMatrix(floor((i-1)/(testSize-2)) + 1,minimum) = confusionMatrix(floor((i-1)/(testSize-2))+1);
end
以下是我的全部代码。用于基于testSets和trainSets的语音识别。
TrainSet=dir('TrainSet');
maxLength=0;
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
sample=wavread(path); % Reading Files
if(length(sample) > maxLength)
maxLength = length(sample);
end
end
end
TestSet=dir('TestSet');
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
sample=wavread(path);
if(length(sample) > maxLength)
maxLength = length(sample);
end
end
end
%Equalizing Trainset data
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
trainSize = length(sub_direction);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
trainSetMatrix(i-2,j-2,:) = zeros(1,maxLength);
sample=wavread(path); % Reading Files
for k=1:length(sample)
trainSetMatrix(i-2,j-2,k) = sample(k);
end
end
end
%Equalizing Testset data
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
testSize = length(sub_direction);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
testSetMatrix(i-2,j-2,:) = zeros(1,maxLength);
sample=wavread(path); % Reading Files
for k=1:length(sample)
testSetMatrix(i-2,j-2,k) = sample(k);
end
end
end
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
frameSize = 160;%framing: each frame equals to 8khz*20msec = 160samples
frameOverlap = 80;%overlapping is 50% of each frame
framedTrainSetMatrix(1,:) = trainSetMatrix(i-2,j-2,1:160);
for k=1:floor(maxLength/80)-2
framedTrainSetMatrix(k+1,:)= trainSetMatrix(i-2,j-2,k*80:(k*80+frameSize-1));
end
end
end
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
window = hamming(frameSize);
for k=1:floor(maxLength/80)-1
windowedFrame(k,:) = framedTrainSetMatrix(k,:).*window';
LinearPredictiveCoding(k,:) = lpc(windowedFrame(k,:),12);
lpcResult(k,:) = LinearPredictiveCoding(k,2:13);
end
coefficient=12*(floor(maxLength/80)-1);
X((i-3)*(trainSize-2)+(j-2),:)=reshape(lpcResult,1,coefficient);
end
end
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
frameSize = 160;%framing: each frame equals to 8khz*20msec = 160samples
frameOverlap = 80;%overlapping is 50% of each frame
framedTestSetMatrix(1,:) = testSetMatrix(i-2,j-2,1:160);
for k=1:floor(maxLength/80)-2
framedTestSetMatrix(k+1,:)= testSetMatrix(i-2,j-2,k*80:(k*80+frameSize-1));
end
end
end
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
window = hamming(frameSize);
for k=1:floor(maxLength/80)-1
windowedFrame(k,:) = framedTestSetMatrix(k,:).*window';
LinearPredictiveCoding(k,:) = lpc(windowedFrame(k,:),12);
lpcResult(k,:) = LinearPredictiveCoding(k,2:13);
end
coefficient=12*(floor(maxLength/80)-1);
Y((i-3)*(testSize-2)+(j-2),:)=reshape(lpcResult,1,coefficient);
end
end
Xt = transpose(X);
T(10,1900);
hiddelLayer=1158;
net=newff(Xt,T,hiddelLayer);%building network
net.divideParam.trainRatio=0.2;
net.efficiency.memoryReduction=60;
net=train(net,Xt,T);%training network
Yt = transpose(Y);
output = sim(net,Yt);%testing network
%building confusion matrix
confusionMatrix(10,10);
for i=1:(testSize-2)*10
for j=1:10
m(j) = abs(output(i,i)-1);
end
minimum = find(m == min(m));
confusionMatrix(floor((i-1)/(testSize-2)) + 1,minimum) = confusionMatrix(floor((i-1)/(testSize-2))+1);
end
for i=1:10
for j=1:10
confusionMatrix(i,j) = confusionMatrix(i,j)/(testSize-2);
end
end
完整的错误是:
??? Error using ==> eq
Matrix dimensions must agree.
Error in ==> test at 167
minimum = find(m == min(m));
请帮忙。
如果你想找到你最好使用的最小参数的索引
[mn mIdx] = min( m(:) );
而不是
mIdx = find( m == min(m(:)) );
有关 Matlab 的 argmax 和 argmin 的更多信息,请参阅 this thread。
请参阅 min
的文档:
If A is a matrix, then min(A) is a row vector containing the minimum
value of each column.
您的 min(m)
调用返回一个向量,MATLAB 不能将其与 ==
一起使用(==
的输入 A 和 B 的大小必须相同,除非一个是标量) ,如本测试用例所示,它会产生相同的错误:
a = rand(4);
test = find(a == min(a));
如果要查找整个矩阵中最小值的索引,请使用 find(a == min(a(:)));
或者更简单地说:[minval idx] = min(a(:))
我知道以前有人问过这个问题,但我没有从他们那里得到答案。而且我也没有从 mathworks.com 那里得到太多 我正在尝试为 ANN 构建混淆矩阵。 testSize 是我拥有的测试集文件的数量。
output = sim(net,Yt);%testing network
confusionMatrix(10,10);%building confusion matrix
for i=1:(testSize-2)*10
for j=1:10
m(j) = abs(output(i,i)-1);
end
minimum = find(m == min(m));
confusionMatrix(floor((i-1)/(testSize-2)) + 1,minimum) = confusionMatrix(floor((i-1)/(testSize-2))+1);
end
以下是我的全部代码。用于基于testSets和trainSets的语音识别。
TrainSet=dir('TrainSet');
maxLength=0;
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
sample=wavread(path); % Reading Files
if(length(sample) > maxLength)
maxLength = length(sample);
end
end
end
TestSet=dir('TestSet');
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
sample=wavread(path);
if(length(sample) > maxLength)
maxLength = length(sample);
end
end
end
%Equalizing Trainset data
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
trainSize = length(sub_direction);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
trainSetMatrix(i-2,j-2,:) = zeros(1,maxLength);
sample=wavread(path); % Reading Files
for k=1:length(sample)
trainSetMatrix(i-2,j-2,k) = sample(k);
end
end
end
%Equalizing Testset data
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
testSize = length(sub_direction);
for j=3:length(sub_direction)
path=strcat(File,'\',sub_direction(j).name);
testSetMatrix(i-2,j-2,:) = zeros(1,maxLength);
sample=wavread(path); % Reading Files
for k=1:length(sample)
testSetMatrix(i-2,j-2,k) = sample(k);
end
end
end
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
frameSize = 160;%framing: each frame equals to 8khz*20msec = 160samples
frameOverlap = 80;%overlapping is 50% of each frame
framedTrainSetMatrix(1,:) = trainSetMatrix(i-2,j-2,1:160);
for k=1:floor(maxLength/80)-2
framedTrainSetMatrix(k+1,:)= trainSetMatrix(i-2,j-2,k*80:(k*80+frameSize-1));
end
end
end
for i=3:length(TrainSet)
File=strcat('TrainSet\',TrainSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
window = hamming(frameSize);
for k=1:floor(maxLength/80)-1
windowedFrame(k,:) = framedTrainSetMatrix(k,:).*window';
LinearPredictiveCoding(k,:) = lpc(windowedFrame(k,:),12);
lpcResult(k,:) = LinearPredictiveCoding(k,2:13);
end
coefficient=12*(floor(maxLength/80)-1);
X((i-3)*(trainSize-2)+(j-2),:)=reshape(lpcResult,1,coefficient);
end
end
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
frameSize = 160;%framing: each frame equals to 8khz*20msec = 160samples
frameOverlap = 80;%overlapping is 50% of each frame
framedTestSetMatrix(1,:) = testSetMatrix(i-2,j-2,1:160);
for k=1:floor(maxLength/80)-2
framedTestSetMatrix(k+1,:)= testSetMatrix(i-2,j-2,k*80:(k*80+frameSize-1));
end
end
end
for i=3:length(TestSet)
File=strcat('TestSet\',TestSet(i).name);
sub_direction=dir(File);
for j=3:length(sub_direction)
window = hamming(frameSize);
for k=1:floor(maxLength/80)-1
windowedFrame(k,:) = framedTestSetMatrix(k,:).*window';
LinearPredictiveCoding(k,:) = lpc(windowedFrame(k,:),12);
lpcResult(k,:) = LinearPredictiveCoding(k,2:13);
end
coefficient=12*(floor(maxLength/80)-1);
Y((i-3)*(testSize-2)+(j-2),:)=reshape(lpcResult,1,coefficient);
end
end
Xt = transpose(X);
T(10,1900);
hiddelLayer=1158;
net=newff(Xt,T,hiddelLayer);%building network
net.divideParam.trainRatio=0.2;
net.efficiency.memoryReduction=60;
net=train(net,Xt,T);%training network
Yt = transpose(Y);
output = sim(net,Yt);%testing network
%building confusion matrix
confusionMatrix(10,10);
for i=1:(testSize-2)*10
for j=1:10
m(j) = abs(output(i,i)-1);
end
minimum = find(m == min(m));
confusionMatrix(floor((i-1)/(testSize-2)) + 1,minimum) = confusionMatrix(floor((i-1)/(testSize-2))+1);
end
for i=1:10
for j=1:10
confusionMatrix(i,j) = confusionMatrix(i,j)/(testSize-2);
end
end
完整的错误是:
??? Error using ==>
eq
Matrix dimensions must agree.Error in ==> test at 167
minimum = find(m == min(m));
请帮忙。
如果你想找到你最好使用的最小参数的索引
[mn mIdx] = min( m(:) );
而不是
mIdx = find( m == min(m(:)) );
有关 Matlab 的 argmax 和 argmin 的更多信息,请参阅 this thread。
请参阅 min
的文档:
If A is a matrix, then min(A) is a row vector containing the minimum value of each column.
您的 min(m)
调用返回一个向量,MATLAB 不能将其与 ==
一起使用(==
的输入 A 和 B 的大小必须相同,除非一个是标量) ,如本测试用例所示,它会产生相同的错误:
a = rand(4);
test = find(a == min(a));
如果要查找整个矩阵中最小值的索引,请使用 find(a == min(a(:)));
或者更简单地说:[minval idx] = min(a(:))