【问题标题】:error: Matrix dimensions must agree in matlab for building confusion matrix错误:矩阵维度必须在 matlab 中一致才能构建混淆矩阵
【发布时间】:2015-05-29 11:04:11
【问题描述】:

我知道以前有人问过这个问题,但我没有从他们那里得到答案。而且我也没有从 mathworks.com 得到什么 我正在尝试为 ANN 构建混淆矩阵。 testSize 是我拥有的 testSet 文件的数量。

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

完整的错误是:

???错误使用 ==> eq
矩阵尺寸必须一致。

==> 167 测试中的错误
最小值 = find(m == min(m));

请帮忙。

【问题讨论】:

  • 请提供完整的错误和堆栈跟踪。
  • 我编辑了它。你可以看到完整的错误。

标签: matlab neural-network speech-recognition confusion-matrix


【解决方案1】:

查看min的文档:

如果 A 是矩阵,则 min(A) 是包含最小值的行向量 每列的值。

您的 min(m) 调用返回一个向量,MATLAB 不能将其与 == 一起使用(== 的输入 A 和 B 必须相同大小,除非是标量),如此测试用例所示,这会产生相同的错误:

a = rand(4);
test = find(a == min(a));

如果您要查找整个矩阵中最小值的索引,请使用find(a == min(a(:)));

或者,更简单地说:[minval idx] = min(a(:))

【讨论】:

  • 谢谢。然而我得到另一个错误:???试图访问输出(11,11);索引超出范围,因为 size(output)=[10,460]。 ==> 165 m(j) = abs(output(i,i)-1) 处的测试错误;
  • @abcd 看看错误告诉你什么。当 output 只有 10 行时,您正尝试访问第 11 行。您需要重新评估您的索引。
【解决方案2】:

如果你想找到你最好使用的最小参数的索引

[mn mIdx] = min( m(:) ); 

而不是

mIdx = find( m == min(m(:)) );

有关 Matlab 的 argmax 和 argmin 的更多信息,请参阅 this thread。

【讨论】:

  • 谢谢。然而我得到另一个错误:???试图访问输出(11,11);索引超出范围,因为 size(output)=[10,460]。 ==> 在 165 m(j) = abs(output(i,i)-1) 处测试时出错;
猜你喜欢
  • 1970-01-01
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 2015-01-20
相关资源
最近更新 更多