如何自动循环组合

时间:2013-04-18 10:32:34

标签: matlab

新版本:

主程序和功能的编辑部分

AID=[30,50,52,53,54,56,57,72,75,77];
SID=[30,50,52,53,54,56,57,72,75,77];
[AID,SID]=meshgrid(AID,SID)
myfunction=@(SID,AID)myfunc(Blink,SID,AID);                                    
[rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,y1,y2,y3,y4]=arrayfun(Blink,AID,SID)

功能

function [rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,y1,y2,y3,y4] = arrayfun(Blink,AID,SID)
for i=1:length(BlinkSetList)
 S=cell2mat(BlinkSetList(i));                                 
   for j=1:length(S)
     if S(j).AID==AID & S(j).SID==SID       
        if S(j).AnchorChan==0 & S(j).SourceChan==0             
        y=S(j).agc;                                             
        rss_dB1(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB1(i)=0;
            isempty(rss_dB1(i))              
        end
        if S(j).AnchorChan==0 & S(j).SourceChan==1            
        y=S(j).agc;                                             
        rss_dB2(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB2(i)=0;
            isempty(rss_dB2(i))
        end
        if S(j).AnchorChan==1 & S(j).SourceChan==0             
        y=S(j).agc;                                             
        rss_dB3(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB3(i)=0;
            isempty(rss_dB3(i))
        end
        if S(j).AnchorChan==1 & S(j).SourceChan==1             
        y=S(j).agc;                                            
        rss_dB4(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB4(i)=0;
            isempty(rss_dB4(i))
        end
     end           
   end
end
   rss_dB1(rss_dB1==0)=[];
   rss_dB2(rss_dB2==0)=[];
   rss_dB3(rss_dB3==0)=[];
   rss_dB4(rss_dB4==0)=[];
   y1=std(rss_dB1);
   y2=std(rss_dB2);
   y3=std(rss_dB3);
   y4=std(rss_dB4);
   rss_dBm1=sum(rss_dB1(:))/length(rss_dB1);
   rss_dBm2=sum(rss_dB2(:))/length(rss_dB2);
   rss_dBm3=sum(rss_dB3(:))/length(rss_dB3);
   rss_dBm4=sum(rss_dB4(:))/length(rss_dB4);

    disp([sprintf('The rssi value  with A-Chan 0 and S-Chan 0 is %0.0f',rss_dBm1)]);
    disp([sprintf('The rssi value  with A-Chan 0 and S-Chan 1 is %0.0f',rss_dBm2)]);
    disp([sprintf('The rssi value  with A-Chan 1 and S-Chan 0 is %0.0f',rss_dBm3)]);
    disp([sprintf('The rssi value  with A-Chan 1 and S-Chan 1 is %0.0f',rss_dBm4)]);

我输出后跟错误

AID =

  Columns 1 through 10
  30 50 52........ 
  30 50 52 
  30 50 52
  .
  .

SID =

  Columns 1 through 10

  30        30          30                  
  50        50          50          
  52        52          52......   
  .
  .
  .

???未定义的函数或变量“rss_dB1”。      ==>中的错误arrayfun 54岁      rss_dB1(rss_dB1 == 0)= [];

Error in ==> main_reduced at 38

[rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,Y1,Y2,Y3,Y4] = arrayfun(闪烁,AID,SID)

但我希望我的结果为

所有组合的结果例如:AID = 30 SID = 50,AID = 50 SID = 54,AID = 54 SID = 57 .......

The rss value  with A-Chan 0 and S-Chan 0 is -68 % combination of AID=30 SID=50 
The rss value  with A-Chan 0 and S-Chan 1 is -73 % with all pairs of anchor and source channel (0,0),(0,1),(1,0),(1,1)
The rss value  with A-Chan 1 and S-Chan 0 is -73
The rss value  with A-Chan 1 and S-Chan 1 is -76
The rss value  with A-Chan 0 and S-Chan 0 is -68 % combination of AID=50 SID=54 
The rss value  with A-Chan 0 and S-Chan 1 is -73% with all pairs of anchor and source channel (0,0),(0,1),(1,0),(1,1)
The rss value  with A-Chan 1 and S-Chan 0 is -73
The rss value  with A-Chan 1 and S-Chan 1 is -76
The rss value  with A-Chan 0 and S-Chan 0 is -68 % combination of AID=54 SID=57 
The rss value  with A-Chan 0 and S-Chan 1 is -73 % with all pairs of anchor and source channel (0,0),(0,1),(1,0),(1,1)
The rss value  with A-Chan 1 and S-Chan 0 is -73
The rss value  with A-Chan 1 and S-Chan 1 is -76

rss_dBm1 =-68
rss_dBm2 =-72.8621
rss_dBm3 =-73
rss_dBm4 = -76
rss_dBm1 =-68
rss_dBm2 =-72.8621
rss_dBm3 =-73
rss_dBm4 = -76
rss_dBm1 =-68
rss_dBm2 =-72.8621
rss_dBm3 =-73
rss_dBm4 = -76

注意:有时频道对组合或AID& SID组合不存在,所以在这种情况下它只返回NaN(这就是我使用isempty的原因)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

首先,我感谢任何看过这篇文章并尝试给出解决方案的人。谢谢提前

我的问题如下,

主程序的一部分

 AID=30;
 SID=50;

[rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,Y1,Y2,Y3,Y4] =样品(闪烁,AID,SID)

注意:AID有不同的ID为30,50,52,54,55,57(与SID相同)          SID具有不同的ID,如30,50,52,54,55,57(与AID相同)

  Here the AID and SID is manually entered from the user to check the below anchorchannel and source channel condition and check if such combination exist it will display the rss values if not it will return NaN.

调用函数

 function [rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,y1,y2,y3,y4]=sample(Blink,AID,SID)        


 for i=1:length(Blink)     %Blink=<500x1 cell> inside which several blinks are present
 S=cell2mat(Blink(i));     %  with information on AID,SID,agc
   for j=1:length(S)
    if S(j).AID==AID && S(j).SID==SID      
       if S(j).AnchorChannel==0 && S(j).SourceChannel==0 %Anchor-source channel                                   
        y=S(j).agc; %agc is present in every blink to calculate rss       %combination                                           
        rss_dB1(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB1(i)=0;
            isempty(rss_dB1(i))              
        end
        if S(j).AnchorChannel==0 && S(j).SourceChannel==1             
        y=S(j).agc;                                            
        rss_dB2(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB2(i)=0;
            isempty(rss_dB2(i))
        end
        if S(j).AnchorChannel==1 && S(j).SourceChannel==0            
        y=S(j).agc;                                            
        rss_dB3(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB3(i)=0;
            isempty(rss_dB3(i))
        end
        if S(j).AnchorChan==1 && S(j).SourceChan==1             
        y=S(j).agc;                                            
        rss_dB4(i)= -(33+y*(89-33)/(29-1));
        else
            rss_dB4(i)=0;
            isempty(rss_dB4(i))
        end
    end
  end
end


   rss_dB1(rss_dB1==0)=[];
   rss_dB2(rss_dB2==0)=[];
   rss_dB3(rss_dB3==0)=[];
   rss_dB4(rss_dB4==0)=[];
   y1=std(rss_dB1);
   y2=std(rss_dB2);
   y3=std(rss_dB3);
   y4=std(rss_dB4);
   rss_dBm1=sum(rss_dB1(:))/length(rss_dB1);
   rss_dBm2=sum(rss_dB2(:))/length(rss_dB2);
   rss_dBm3=sum(rss_dB3(:))/length(rss_dB3);
   rss_dBm4=sum(rss_dB4(:))/length(rss_dB4);

    disp([sprintf('The rss value  with A-Chan 0 and S-Chan 0 is %0.0f',rss_dBm1)]);
    disp([sprintf('The rss value  with A-Chan 0 and S-Chan 1 is %0.0f',rss_dBm2)]);
    disp([sprintf('The rss value  with A-Chan 1 and S-Chan 0 is %0.0f',rss_dBm3)]);
    disp([sprintf('The rss value  with A-Chan 1 and S-Chan 1 is %0.0f',rss_dBm4)]);

现在我的问题是如何在不给用户输入的情况下自动检查AID和SID的不同组合?如果这有意义,它应循环遍历每个组合并返回AID SID与锚定通道和源通道的所有可能组合的“rss”结果

一个组合的结果:AID = 30 SID = 50

The rss value  with A-Chan 0 and S-Chan 0 is -68
The rss value  with A-Chan 0 and S-Chan 1 is -73
The rss value  with A-Chan 1 and S-Chan 0 is -73
The rss value  with A-Chan 1 and S-Chan 1 is -76

rss_dBm1 =-68
rss_dBm2 =-72.8621
rss_dBm3 =-73
rss_dBm4 = -76


y1 = 1.4142
y2 =  1.4072
y3 =   0
y4 =  1.1547

以上是一个AID(30)SID(50)组合的结果。但我想循环,如AID = 50 SID = 52,AID = 52 SID = 55,AID = 57 SID = 54所以这些是对的一些例子。我希望结果类似于上面的输出,除了它还应该包括我提到的对,有四种不同的通道组合

注意:上述组合的输出也必须包含在下面提到的组合中。 示例:AID = 50 SID = 52,AID = 52 SID = 55,AID = 57 SID = 54,包含锚,源通道对(0,0),(0,1),(1,0),(1,1) )在少数情况下,锚源通道对不存在,因此它会自动返回'0'或'NaN'

1 个答案:

答案 0 :(得分:1)

假设您有一个带有sidaid参数的函数,并返回一个包含您需要的所有数据的结构:

res = function sample(Blink, AID, SID)

正如我所说,res这是一个包含字段rss_dBm1rss_dBm2等的结构...

你还有两个阵列:

SIDS = [30,50,52,54,55,57];
AIDS = [30,50,52,54,55,57];

要获得所有sids和aids对,您可以use meshgrid function

[sid aid] = meshgrid(SIDS, AIDS);

要为每对调用您的函数,您可以use arrayfun function

fn = @(sid, aid) sample(Blink, sid, aid);
data = arrayfun(fn, sid, aid);

data这里是length(SIDS) x length(AIDS)结构矩阵。我在这里使用了anonymous function (lambda expression)来对sample函数的第一个参数进行partial application

如果您不想更改功能,可以按原样使用:

fn = @(sid, aid) sample(Blink, sid, aid);
[rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,y1,y2,y3,y4] = arrayfun(fn, sid, aid);

在这种情况下,每个返回的变量都是length(SIDS) x length(AIDS)数组。