开关盒不工作matlab

时间:2014-07-29 19:20:45

标签: matlab switch-statement case

我在MATLAB中编写一个函数,根据一个列值计算一些矩阵。

例如

B=
1.0000    0  0.3000
0         0  0.3000

等等。第3列将在矩阵之间变化,但在整个矩阵中将是相同的。第三列可以具有值.2:.05:.8,我有一个案例。

我遇到了一些非常奇怪的结果。当多次运行交换机外壳时,.3的情况永远不会起作用,即使我可以清楚地看到switch语句被指定为.3。我已经开启了更高精度的调查,开关变量和情况肯定是相同的。

我已在下面粘贴了更完整的代码部分。函数 twolayer 返回[0/1 0/1 bitrate],比特率为switch变量。在实施开关盒时,请告诉我是否有任何错误。我使用if elseif尝试了相同的代码并遇到了同样的问题,我相信这是MATLAB如何比较数字的问题。提前谢谢!

for k=1:15
    A(k,1:3)=twolayer(j,X(1,i));
end  
%Sort the matrix by bitrate
B=sortrows(A,3);
ref=B(1,3);
%find the index at which the sorted matrix switches bitrate
c=1;
while B(c,3)==ref 
    c=c+1;
end
c=c-1;
%Split the matrix by indicated bitrate
C(1:c,1:3)=B(1:c,1:3)
D(1:(15-c),1:3)=B((c+1):15,1:3)
c=0;
%Check each matrix by bitrate then add it's average and
%record the increase in counter
check = double(C(1,3))
switch check
case 0.2000
    cbrbaselayer(j-2,1)=(cbrbaselayer(j-2,1)+(mean(C(:,1))));
    counterone=counterone+1;
    'Counter One Increased'
case  0.2500
    cbrbaselayer(j-2,2)=(cbrbaselayer(j-2,2)+(mean(C(:,1))));
    countertwo=countertwo+1;
    'Counter Two Increased'
case  0.3000
    cbrbaselayer(j-2,3)=(cbrbaselayer(j-2,3)+(mean(C(:,1))));
    counterthree=counterthree+1;
    'Counter Three Increased'
case  0.3500
    cbrbaselayer(j-2,4)=(cbrbaselayer(j-2,4)+(mean(C(:,1))));
    counterfour=counterfour+1;
    'Counter Four Increased'
case  0.4000
    cbrbaselayer(j-2,5)=(cbrbaselayer(j-2,5)+(mean(C(:,1))));
    counterfive=counterfive+1;
    'Counter Five Increased'
case  0.4500
    cbrbaselayer(j-2,6)=(cbrbaselayer(j-2,6)+(mean(C(:,1))));
    countersix=countersix+1;
    'Counter Six Increased'
case  0.5500
        cbrbaselayer(j-2,8)=(cbrbaselayer(j-2,8)+(mean(C(:,1))));
    countereight=countereight+1;
    'Counter Eight Increased'
case  0.6000
    cbrbaselayer(j-2,9)=(cbrbaselayer(j-2,9)+(mean(C(:,1))));
    counternine=counternine+1;
    'Counter Nine Increased'
case  0.6500
    cbrbaselayer(j-2,10)=(cbrbaselayer(j-2,10)+(mean(C(:,1))));
    ounterten=counterten+1;
    'Counter Ten Increased'
case  0.7000
    cbrbaselayer(j-2,11)=(cbrbaselayer(j-2,11)+(mean(C(:,1))));
    countereleven=countereleven+1;
    'Counter Eleven Increased'
case  0.7500
    cbrbaselayer(j-2,12)=(cbrbaselayer(j-2,12)+(mean(C(:,1))));
    countertwelve=countertwelve+1;
    'Counter Twelve Increased'
case  0.8000
    cbrbaselayer(j-2,13)=(cbrbaselayer(j-2,13)+(mean(C(:,1))));
    counterthirteen=counterthirteen+1;
    'Counter Thirteen Increased'
end
check = double(D(1,3))
switch check
case 0.2000
    cbrbaselayer(j-2,1)=(cbrbaselayer(j-2,1)+(mean(D(:,1))));
    counterone=counterone+1;
    'Counter One Increased'
case  0.2500
    cbrbaselayer(j-2,2)=(cbrbaselayer(j-2,2)+(mean(D(:,1))));
    countertwo=countertwo+1;
    'Counter Two Increased'
case  0.3000
    cbrbaselayer(j-2,3)=(cbrbaselayer(j-2,3)+(mean(D(:,1))));
    counterthree=counterthree+1;
    'Counter Three Increased'
case  0.3500
    cbrbaselayer(j-2,4)=(cbrbaselayer(j-2,4)+(mean(D(:,1))));
    counterfour=counterfour+1;
    'Counter Four Increased'
case  0.4000
    cbrbaselayer(j-2,5)=(cbrbaselayer(j-2,5)+(mean(D(:,1))));
    counterfive=counterfive+1;
    'Counter Five Increased'
case  0.4500
    cbrbaselayer(j-2,6)=(cbrbaselayer(j-2,6)+(mean(D(:,1))));
    countersix=countersix+1;
    'Counter Six Increased'
case  0.5500
    cbrbaselayer(j-2,8)=(cbrbaselayer(j-2,8)+(mean(D(:,1))));
    countereight=countereight+1;
    'Counter Eight Increased'
case  0.6000
    cbrbaselayer(j-2,9)=(cbrbaselayer(j-2,9)+(mean(D(:,1))));
    counternine=counternine+1;
    'Counter Nine Increased'
case  0.6500
    cbrbaselayer(j-2,10)=(cbrbaselayer(j-2,10)+(mean(D(:,1))));
    counterten=counterten+1;
    'Counter Ten Increased'
case  0.7000
    cbrbaselayer(j-2,11)=(cbrbaselayer(j-2,11)+(mean(D(:,1))));
    countereleven=countereleven+1;
    'Counter Eleven Increased'
case  0.7500
    cbrbaselayer(j-2,12)=(cbrbaselayer(j-2,12)+(mean(D(:,1))));
    countertwelve=countertwelve+1;
    'Counter Twelve Increased'
case  0.8000
    cbrbaselayer(j-2,13)=(cbrbaselayer(j-2,13)+(mean(D(:,1))));
    counterthirteen=counterthirteen+1;
    'Counter Thirteen Increased'
end
clear A; clear B; clear C; clear D; clear check; clear c;

1 个答案:

答案 0 :(得分:1)

首先代码很长。编码是为了让您的生活更轻松。不难。

这是我建议改进switch语句,写两个子函数,一个用于C,一个用于D

function checkC(inputvalue)
 index = (inputvalue-0.2)/0.05 +1;
 cbrbaselayer(j-2,index)=(cbrbaselayer(j-2,index)+(mean(C(:,1))));
 eval(sprintf(counter%d = counter%d +1,index,index));
end

function checkD( inputvalue)
 index = (inputvalue-0.2)/0.05 +1;
 cbrbaselayer(j-2,index)=(cbrbaselayer(j-2,index)+(mean(D(:,1))));
 eval(sprintf(counter%d = counter%d +1,index,index));
end