Matlab:摆脱“a”;开关/外壳“;在我的代码范围内
您不需要阅读所有这些代码,但我将其发布以供参考。我需要将下面的代码段从案例0-8更改为仅针对n。在不同的情况下,唯一会发生变化的是z的范围和变量名(msgLength0、msgLength1、msgLength2等)。我尝试将变量msgLength#放入一个数组中,但如果没有“count”变量,就无法将数据分配到正确的行 我如何摆脱这个“开关”,使用变量,而不是在个别情况下单独定义msgLengthMatlab:摆脱“a”;开关/外壳“;在我的代码范围内,matlab,Matlab,您不需要阅读所有这些代码,但我将其发布以供参考。我需要将下面的代码段从案例0-8更改为仅针对n。在不同的情况下,唯一会发生变化的是z的范围和变量名(msgLength0、msgLength1、msgLength2等)。我尝试将变量msgLength#放入一个数组中,但如果没有“count”变量,就无法将数据分配到正确的行 我如何摆脱这个“开关”,使用变量,而不是在个别情况下单独定义msgLength for these folders for these files cou
for these folders
for these files
countmsgLength0= 1;
countmsgLength1= 1;
countmsgLength2= 1;
countmsgLength3= 1;
countmsgLength4= 1;
countmsgLength5= 1;
countmsgLength6= 1;
countmsgLength7= 1;
countmsgLength8= 1;
for x= 1:length(firstinSeq)
for y= 1:length(littledataPassed)
if firstinSeq(x,1)== littledataPassed(y,1) && firstinSeq(x,2)== littledataPassed(y,2)
switch firstinSeq(x,3)
case 0
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == 0),1,'first');
if isempty(indexProcess)
msgLength0(countmsgLength0,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength0(countmsgLength0,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength0= countmsgLength0 + 1;
case 1
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == 0), 1, 'first');
if isempty(indexProcess)
msgLength1(countmsgLength1,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength1(countmsgLength1,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength1= countmsgLength1 + 1;
case 2
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:1
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength2(countmsgLength2,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength2(countmsgLength2,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength2= countmsgLength2 + 1;
end
case 3
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:2
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength3(countmsgLength3,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength3(countmsgLength3,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength3= countmsgLength3 + 1;
end
case 4
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z=0:3
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength4(countmsgLength4,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength4(countmsgLength4,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength4= countmsgLength4 + 1;
end
case 5
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:4
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength5(countmsgLength5,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength5(countmsgLength5,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength5= countmsgLength5 + 1;
end
case 6
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:5
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength6(countmsgLength6,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength6(countmsgLength6,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength6= countmsgLength6 + 1;
end
case 7
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z=0:6
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength7(countmsgLength7,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength7(countmsgLength7,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength7= countmsgLength7 + 1;
end
case 8
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z=0:7
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength8(countmsgLength8,:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength8(countmsgLength8,:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength8= countmsgLength8+ 1;
end
end
break
end
end
end
msgLength0((countmsgLength0):100,:)= [];
msgLength1((countmsgLength1):1000,:)= [];
msgLength2((countmsgLength2):1000,:)= [];
msgLength3((countmsgLength3):1500,:)= [];
msgLength4((countmsgLength4):1000,:)= [];
msgLength5((countmsgLength5):1000,:)= [];
msgLength6((countmsgLength6):500,:)= [];
msgLength7((countmsgLength7):300,:)= [];
msgLength8((countmsgLength8):300,:)= [];
allmsgLength0(countallmsgLength0:(countallmsgLength0+length(msgLength0(:,1))-1),:) = msgLength0;
allmsgLength1(countallmsgLength1:(countallmsgLength1+length(msgLength1(:,1))-1),:) = msgLength1;
allmsgLength2(countallmsgLength2:(countallmsgLength2+length(msgLength2(:,1))-1),:) = msgLength2;
allmsgLength3(countallmsgLength3:(countallmsgLength3+length(msgLength3(:,1))-1),:) = msgLength3;
allmsgLength4(countallmsgLength4:(countallmsgLength4+length(msgLength4(:,1))-1),:) = msgLength4;
allmsgLength5(countallmsgLength5:(countallmsgLength5+length(msgLength5(:,1))-1),:) = msgLength5;
allmsgLength6(countallmsgLength6:(countallmsgLength6+length(msgLength6(:,1))-1),:) = msgLength6;
allmsgLength7(countallmsgLength7:(countallmsgLength7+length(msgLength7(:,1))-1),:) = msgLength7;
allmsgLength8(countallmsgLength8:(countallmsgLength8+length(msgLength8(:,1))-1),:) = msgLength8;
countallmsgLength0 = countallmsgLength0 + length(msgLength0(:,1));
countallmsgLength1 = countallmsgLength1 + length(msgLength1(:,1));
countallmsgLength2 = countallmsgLength2 + length(msgLength2(:,1));
countallmsgLength3 = countallmsgLength3 + length(msgLength3(:,1));
countallmsgLength4 = countallmsgLength4 + length(msgLength4(:,1));
countallmsgLength5 = countallmsgLength5 + length(msgLength5(:,1));
countallmsgLength6 = countallmsgLength6 + length(msgLength6(:,1));
countallmsgLength7 = countallmsgLength7 + length(msgLength7(:,1));
countallmsgLength8 = countallmsgLength8 + length(msgLength8(:,1));
end
cd ..
end
您是否可以尝试将数组用于
countMsgLength
,例如,您可以增加countMsgLength(4)
,而不是countMsgLength4
由于msgLengthN
似乎是矩阵,如果所有msgLengthN最终都是相同维度的,则可以形成一个类似立方体的3d数组。您只需要在开始时初始化这个数组,然后随着代码的进行设置适当的元素。或者,如果msgLengthN
s的维度不同,可以使用单元格数组,设置msgLength{N}(countmsgLength(N),:)=
而不是像上面那样设置msgLengthN(countmsgLengthN,:)
。在每次使用之前,将其初始化为空矩阵,例如,在开始时
msgLength = {};
for k=1:8
msgLength{k} = [];
end
或者,如果你提前知道维度,你可以将其转换为零矩阵
msgLength = {};
for k=1:8
msgLength{k} = zeros([rows cols]);
end
除此之外,如果没有一些额外的解释,似乎很难说明您正在尝试做什么。您是否可以尝试将数组用于
countMsgLength
,例如,您可以增加countMsgLength(4)
由于msgLengthN
似乎是矩阵,如果所有msgLengthN最终都是相同维度的,则可以形成一个类似立方体的3d数组。您只需要在开始时初始化这个数组,然后随着代码的进行设置适当的元素。或者,如果msgLengthN
s的维度不同,可以使用单元格数组,设置msgLength{N}(countmsgLength(N),:)=
而不是像上面那样设置msgLengthN(countmsgLengthN,:)
。在每次使用之前,将其初始化为空矩阵,例如,在开始时
msgLength = {};
for k=1:8
msgLength{k} = [];
end
或者,如果你提前知道维度,你可以将其转换为零矩阵
msgLength = {};
for k=1:8
msgLength{k} = zeros([rows cols]);
end
除此之外,如果没有一些额外的解释,似乎很难解释您要做什么。答案是,不要使用这样的变量,在名称中内置数字。它迫使您编写这样混乱的代码。之后,您会发现自己拼命地调试代码。最终,您会发现错误,可能是在复制/粘贴错误中,您键入了一个编号不正确的变量名
相反,使用向量、单元格数组、结构等。将信息存储在数组中,然后索引到该数组/vector/cell/struct中。您的代码将变得更简单、更易于调试、更短。答案是,不要使用这样的变量,在名称中内置数字。它迫使您编写这样混乱的代码。之后,您会发现自己拼命地调试代码。最终,您会发现错误,可能是在复制/粘贴错误中,您键入了一个编号不正确的变量名
相反,使用向量、单元格数组、结构等。将信息存储在数组中,然后索引到该数组/vector/cell/struct中。您的代码将变得更简单、更易于调试、更短。以下是一种摆脱开关/案例的方法。但是,如果您能更多地解释数据的外观以及您希望实现的目标,那么它可能会对您更有用。最有可能的是,整个分析可以简化很多
countmsgLength = ones(9,1); %# countmsgLength(1) are the counts for to case 0
msgLength = cell(9,1);
for x= 1:length(firstinSeq)
for y= 1:length(littledataPassed)
if firstinSeq(x,1)== littledataPassed(y,1) && firstinSeq(x,2)== littledataPassed(y,2)
switchNum = firstinSeq(x,3); %# switchNum is 0...8
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:switchNum-1 %# if switchNum <2, the loop will execute once
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength{switchNum+1}(countmsgLength(switchNum+1),:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength{switchNum+1}(countmsgLength(switchNum+1),:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength(switchNum+1)= countmsgLength(switchNum+1) + 1;
end %# loop z
end %# if
end %# loop y
end %# loop x
countmsgLength=one(9,1);%countmsgLength(1)是案例0的计数
msgLength=单元(9,1);
对于x=1:长度(firstinSeq)
对于y=1:长度(littledataPassed)
如果firstinSeq(x,1)=littledataPassed(y,1)&&firstinSeq(x,2)=littledataPassed(y,2)
switchNum=firstinSeq(x,3);%switchNum是0…8
indexStop=find(firstinSeq(x,4)~=littledataPassed(y:length(littledataPassed),4),1,“first”);
对于z=0:switchNum-1%#如果switchNum,这里有一种方法可以消除开关/案例。但是,如果您能更多地解释数据的外观以及您希望实现的目标,那么它可能会对您更有用。最有可能的是,整个分析可以简化很多
countmsgLength = ones(9,1); %# countmsgLength(1) are the counts for to case 0
msgLength = cell(9,1);
for x= 1:length(firstinSeq)
for y= 1:length(littledataPassed)
if firstinSeq(x,1)== littledataPassed(y,1) && firstinSeq(x,2)== littledataPassed(y,2)
switchNum = firstinSeq(x,3); %# switchNum is 0...8
indexStop = find(firstinSeq(x,4) ~= littledataPassed(y:length(littledataPassed),4), 1, 'first');
for z= 0:switchNum-1 %# if switchNum <2, the loop will execute once
indexProcess = find((littledataPassed(y:y+indexStop-1,6) == 1 & ...
littledataPassed(y:y+indexStop-1,2) == firstinSeq(x,2) & ...
littledataPassed(y:y+indexStop-1,5) == z), 1, 'first');
if isempty(indexProcess)
msgLength{switchNum+1}(countmsgLength(switchNum+1),:)= [firstinSeq(x,:) [0 0 0 0 0 0]];
else msgLength{switchNum+1}(countmsgLength(switchNum+1),:)= [firstinSeq(x,:) littledataPassed(y+indexProcess-1,:)];
end
countmsgLength(switchNum+1)= countmsgLength(switchNum+1) + 1;
end %# loop z
end %# if
end %# loop y
end %# loop x
countmsgLength=one(9,1);%countmsgLength(1)是案例0的计数
msgLength=单元(9,1);
对于x=1:长度(firstinSeq)
对于y=1:长度(littledataPassed)
如果firstinSeq(x,1)=littledataPassed(y,1)&&firstinSeq(x,2)=littledataPassed(y,2)
switchNum=firstinSeq(x,3);%switchNum是0…8
indexStop=find(firstinSeq(x,4)~=littledataPassed(y:length(littledataPassed),4),1,“first”);
对于z=0:switchNum-1%#如果switchNum我不太明白您想要实现什么。你能更详细地解释一下和/或给出一个简单的例子来说明你不太明白你想要实现什么吗。你能更详细地解释一下和/或给出一个简单的例子来说明你想要什么吗。我对matlab和编程都是新手,但大家一致认为这是最好的方法。谢谢。我对matlab和编程都是新手,但大家一致认为这是最好的方法。