1

New version:

The Edited Part of main program and function

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

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)]);

and I get output followed by the error as

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......   
  .
  .
  .

??? Undefined function or variable "rss_dB1". Error in ==> arrayfun at 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(Blink,AID,SID)

but I want my result as

Result for all combinations for example: 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

Note : At times the channel pair combinations or AID & SID combinations does not exist, so in that case it is simply returns NaN ( that's why I used isempty)

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

first of all I appreciate whoever sees this post and try giving a solution.Thanks in advance

My questions is as follows,

A part of main Program

 AID=30;
 SID=50;

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

Note: The AID has different ID's as 30,50,52,54,55,57 (same as SID) The SID has different ID's as 30,50,52,54,55,57 ( same as 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.

calling function

 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)]);

Now my problem is how to automatically check the different combinations of AID and SID without giving the user input ?. if this make sense it should loop over every combinations and return the "rss" result for all possible combinations of AID SID with anchor channel and source channel

Result for one combination: 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

The above is a result for one AID(30) SID(50) combination.But I want to loop over like AID=50 SID=52, AID=52 SID=55, AID=57 SID=54 so these are some examples of the pairs. I want the result to be like the above output, except it should also include the pairs I mentioned ,with four different channel combinations

Note:The output with above combination must also be included with below mentioned combinations too. example:AID=50 SID=52,AID=52 SID=55,AID=57 SID=54 with anchor,source channel pairs (0,0),(0,1),(1,0),(1,1) in few cases the anchor source channel pairs does not exist so then it automatically returns '0'or'NaN'

  • 3
    Maybe [this question](http://stackoverflow.com/questions/7446946/how-to-generate-all-pairs-from-two-vectors-in-matlab-using-vectorised-code) can help? – Eitan T Apr 18 '13 at 10:46
  • Thank you Eitan T ! I will just look into it !! – Vinith p Nair Apr 18 '13 at 10:49
  • Your error message tells me nothing, but redeclaration of the [standard function `arrayfun`](http://www.mathworks.com/help/matlab/ref/arrayfun.html) is the obvious error. Though, it's probably not the only one. – Leonid Beschastny Apr 19 '13 at 15:41
  • Hah, I got what your error means. You renamed `Blink` to `BlinkSetList`, but forgot to change it in the function declaration. – Leonid Beschastny Apr 19 '13 at 15:45
  • @LeonidBeschastny I did change that error after you said but still it shows error !! you are right probably arrayfun is not the only error ! I crossed checked everything but its same thing over and over. I could post the whole code but there are over 30 matlab files and its like 13.8 MB !! – Vinith p Nair Apr 19 '13 at 20:51
  • Check my last comment. You renamed `Blink` to `BlinkSetList`, but kept the old name in the function declaration. So, no data was stored in `rss_dB1` variable. – Leonid Beschastny Apr 20 '13 at 05:27

1 Answers1

1

Suppose you have a function that takes sid and aid arguments and returns a single struct with all the data you need:

res = function sample(Blink, AID, SID)

As I said, res here is a struct with fields rss_dBm1, rss_dBm2, etc...

And you also have two arrays:

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

To obtain all pairs of sids and aids you can use meshgrid function:

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

And to call your function for each pair you can use arrayfun function:

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

data here is a length(SIDS) x length(AIDS) structure matrix. I used here an anonymous function (lambda expression) to do a partial application of the first argument of your sample function.

If you don't want to change you function, you can use it as it is:

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

In this case each returned variable will be a length(SIDS) x length(AIDS) array.

Community
  • 1
  • 1
Leonid Beschastny
  • 50,364
  • 10
  • 118
  • 122
  • this makes some sense maybe I will try it out. Thanx. and I apologize I cannot give you a 1 point because my reputation is too low to give one .. – Vinith p Nair Apr 18 '13 at 10:59
  • @VinithpNair I updated my answer with an additional option for you. – Leonid Beschastny Apr 18 '13 at 11:04
  • Beschanstny : I appreciate it its really helpful – Vinith p Nair Apr 18 '13 at 11:07
  • fn = @(sid, aid) sample(Blink, sid, aid); data = arrayfun(fn, sid, aid); @LeonidBeschastny: I quite did not understand this above command.Should I create another function with this ? and then [rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,y1,y2,y3,y4] = arrayfun(fn, sid, aid); ?? how does this fit in place ? can you edit it in my code it will be good to understand !! – Vinith p Nair Apr 18 '13 at 11:26
  • `@(sid, aid) sample(Blink, sid, aid)` is an inline function declaration. So, `fn(sid,iad)` just calls your `sample` function with preset value of `Blink`. – Leonid Beschastny Apr 18 '13 at 11:41
  • Thank you again. I am just getting used to matlab so pardon for my questions :) – Vinith p Nair Apr 18 '13 at 11:49
  • Actually, `@(sid, aid) sample(Blink, sid, aid)` is an lambda expression (anonymous function). You can read more about anonymous function in MatLab [here](http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html). – Leonid Beschastny Apr 18 '13 at 12:33
  • 1
    actually this makes sense to me. I am just waiting if any one else any different opinions in our discussions. – Vinith p Nair Apr 18 '13 at 12:59
  • Main Function: SIDS = [30,50,52,54,55,57]; AIDS = [30,50,52,54,55,57]; [AnchorID,SourceID]=meshgrid(AnchorID,SourceID) – Vinith p Nair Apr 19 '13 at 12:18
  • Main Function: SIDS = [30,50,52,54,55,57]; AIDS = [30,50,52,54,55,57]; [AIDS,SIDS]=meshgrid(AnchorID,SourceID) myfunction=@(SourceID,AnchorID)myfunc(Blink,SID,AID); [rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,y1,y2,y3,y4]=arrayfun(Blink,AID,SID) Function: function [rss_dBm1,rss_dBm2,rss_dBm3,rss_dBm4,y1,y2,y3,y4] = arrayfun(Blink,AID,SID) but i get errors as ??? Undefined function or variable "rssi_dB1". Error in ==> arrayfun at 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(Blink,AID,SID) – Vinith p Nair Apr 19 '13 at 12:29
  • @VinithpNair I'm sorry, but I can't understand what you trying to tell me. If you have some problems with my solution and want to show me a piece of your code - update your question with it. – Leonid Beschastny Apr 19 '13 at 14:19
  • You can check the edited one which I have pasted for you under the New version title.You can find it in the starting of the page – Vinith p Nair Apr 19 '13 at 15:03