如何将向量转换为标量,用于 switch 语句?
How to convert vector to scalar, for switch statement?
我有一个代码来测试网络的 OCR 过程。
这是:
%% Set new target data (data testing) data2 = fetest8; % data2 = fetest2; % data2 = fetest3; targetx = zeros(1,204); targetx (:,1:50) = 1; targetx (:,51:52) = 2; targetx (:,53:62) = 3; targetx (:,63:82) = 4; targetx (:,83) = 5; targetx (:,84:85) = 6; targetx (:,86:92) = 7; targetx (:,93:95) = 8; targetx (:,96:105) = 9; targetx (:,106) = 10; targetx (:,107:111) = 11; targetx (:,112:116) = 12; targetx (:,117:126) = 13; targetx (:,127:176) = 14; targetx (:,177) = 15; targetx (:,178:180) = 16; targetx (:,181) = 17; targetx (:,182:201) = 18; targetx (:,202:204) = 19;
prediksi = round(sim (net, data2)); %contain target in matrix, data type: double
switch (prediksi)
case '1'
fprintf ('Alif\n');
case '2'
fprintf ('Baa\n');
case '3'
fprintf ('Haaa\n');
case '4'
fprintf ('Daal\n');
case '5'
fprintf ('Raa\n');
case '6'
fprintf ('Siin\n');
case '7'
fprintf ('Saad\n');
case '8'
fprintf ('Thaaa\n');
case '9'
fprintf ('Ayn\n');
case '10'
fprintf ('Faa\n');
case '11'
fprintf ('Gaaf\n');
case '12'
fprintf ('Kaaf\n');
case '13'
fprintf ('Laam\n');
case '14'
fprintf ('Miim\n');
case '15'
fprintf ('Nuun\n');
case '16'
fprintf ('Haa\n');
case '17'
fprintf ('Waaw\n');
case '18'
fprintf ('Yaa\n');
case '19'
fprintf ('Kaaf\n');
otherwise
fprintf ('X\n');
end
prediksi 的屏幕截图:
1 到 204,prediksi 1x24 双。
我收到此错误:'SWITCH expression must be a scalar or string constant.'
如何修复此错误?
如有任何帮助,我们将不胜感激。
这里 prediksi
将是一个大小为 1xm
的行矩阵。所以不能使用 switch(prediksi)
。相反,您必须使用循环并执行 switch (prediksi(i))
。这是更新后的代码
%% Set new target data (data testing) data2 = fetest8; % data2 = fetest2; % data2 = fetest3; targetx = zeros(1,204); targetx (:,1:50) = 1; targetx (:,51:52) = 2; targetx (:,53:62) = 3; targetx (:,63:82) = 4; targetx (:,83) = 5; targetx (:,84:85) = 6; targetx (:,86:92) = 7; targetx (:,93:95) = 8; targetx (:,96:105) = 9; targetx (:,106) = 10; targetx (:,107:111) = 11; targetx (:,112:116) = 12; targetx (:,117:126) = 13; targetx (:,127:176) = 14; targetx (:,177) = 15; targetx (:,178:180) = 16; targetx (:,181) = 17; targetx (:,182:201) = 18; targetx (:,202:204) = 19;
prediksi = round(sim (net, data2)); %contain target in matrix, data type: double
for i=1:size(prediksi,2)
switch (prediksi(i))
case 1
fprintf ('Alif\n');
case 2
fprintf ('Baa\n');
case 3
fprintf ('Haaa\n');
case 4
fprintf ('Daal\n');
case 5
fprintf ('Raa\n');
case 6
fprintf ('Siin\n');
case 7
fprintf ('Saad\n');
case 8
fprintf ('Thaaa\n');
case 9
fprintf ('Ayn\n');
case 10
fprintf ('Faa\n');
case 11
fprintf ('Gaaf\n');
case 12
fprintf ('Kaaf\n');
case 13
fprintf ('Laam\n');
case 14
fprintf ('Miim\n');
case 15
fprintf ('Nuun\n');
case 16
fprintf ('Haa\n');
case 17
fprintf ('Waaw\n');
case 18
fprintf ('Yaa\n');
case 19
fprintf ('Kaaf\n');
otherwise
fprintf ('X\n');
end
end
我有一个代码来测试网络的 OCR 过程。 这是:
%% Set new target data (data testing) data2 = fetest8; % data2 = fetest2; % data2 = fetest3; targetx = zeros(1,204); targetx (:,1:50) = 1; targetx (:,51:52) = 2; targetx (:,53:62) = 3; targetx (:,63:82) = 4; targetx (:,83) = 5; targetx (:,84:85) = 6; targetx (:,86:92) = 7; targetx (:,93:95) = 8; targetx (:,96:105) = 9; targetx (:,106) = 10; targetx (:,107:111) = 11; targetx (:,112:116) = 12; targetx (:,117:126) = 13; targetx (:,127:176) = 14; targetx (:,177) = 15; targetx (:,178:180) = 16; targetx (:,181) = 17; targetx (:,182:201) = 18; targetx (:,202:204) = 19;
prediksi = round(sim (net, data2)); %contain target in matrix, data type: double
switch (prediksi)
case '1'
fprintf ('Alif\n');
case '2'
fprintf ('Baa\n');
case '3'
fprintf ('Haaa\n');
case '4'
fprintf ('Daal\n');
case '5'
fprintf ('Raa\n');
case '6'
fprintf ('Siin\n');
case '7'
fprintf ('Saad\n');
case '8'
fprintf ('Thaaa\n');
case '9'
fprintf ('Ayn\n');
case '10'
fprintf ('Faa\n');
case '11'
fprintf ('Gaaf\n');
case '12'
fprintf ('Kaaf\n');
case '13'
fprintf ('Laam\n');
case '14'
fprintf ('Miim\n');
case '15'
fprintf ('Nuun\n');
case '16'
fprintf ('Haa\n');
case '17'
fprintf ('Waaw\n');
case '18'
fprintf ('Yaa\n');
case '19'
fprintf ('Kaaf\n');
otherwise
fprintf ('X\n');
end
prediksi 的屏幕截图:
1 到 204,prediksi 1x24 双。
如有任何帮助,我们将不胜感激。
这里 prediksi
将是一个大小为 1xm
的行矩阵。所以不能使用 switch(prediksi)
。相反,您必须使用循环并执行 switch (prediksi(i))
。这是更新后的代码
%% Set new target data (data testing) data2 = fetest8; % data2 = fetest2; % data2 = fetest3; targetx = zeros(1,204); targetx (:,1:50) = 1; targetx (:,51:52) = 2; targetx (:,53:62) = 3; targetx (:,63:82) = 4; targetx (:,83) = 5; targetx (:,84:85) = 6; targetx (:,86:92) = 7; targetx (:,93:95) = 8; targetx (:,96:105) = 9; targetx (:,106) = 10; targetx (:,107:111) = 11; targetx (:,112:116) = 12; targetx (:,117:126) = 13; targetx (:,127:176) = 14; targetx (:,177) = 15; targetx (:,178:180) = 16; targetx (:,181) = 17; targetx (:,182:201) = 18; targetx (:,202:204) = 19;
prediksi = round(sim (net, data2)); %contain target in matrix, data type: double
for i=1:size(prediksi,2)
switch (prediksi(i))
case 1
fprintf ('Alif\n');
case 2
fprintf ('Baa\n');
case 3
fprintf ('Haaa\n');
case 4
fprintf ('Daal\n');
case 5
fprintf ('Raa\n');
case 6
fprintf ('Siin\n');
case 7
fprintf ('Saad\n');
case 8
fprintf ('Thaaa\n');
case 9
fprintf ('Ayn\n');
case 10
fprintf ('Faa\n');
case 11
fprintf ('Gaaf\n');
case 12
fprintf ('Kaaf\n');
case 13
fprintf ('Laam\n');
case 14
fprintf ('Miim\n');
case 15
fprintf ('Nuun\n');
case 16
fprintf ('Haa\n');
case 17
fprintf ('Waaw\n');
case 18
fprintf ('Yaa\n');
case 19
fprintf ('Kaaf\n');
otherwise
fprintf ('X\n');
end
end