matlab - How to automatically loop over the combinations -


this question has answer here:

new version:

the edited part of main program , 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) i=1:length(blinksetlist)  s=cell2mat(blinksetlist(i));                                     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  a-chan 0 , s-chan 0 %0.0f',rss_dbm1)]);     disp([sprintf('the rssi value  a-chan 0 , s-chan 1 %0.0f',rss_dbm2)]);     disp([sprintf('the rssi value  a-chan 1 , s-chan 0 %0.0f',rss_dbm3)]);     disp([sprintf('the rssi value  a-chan 1 , s-chan 1 %0.0f',rss_dbm4)]); 

and output followed 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 @ 54 rss_db1(rss_db1==0)=[];

error in ==> main_reduced @ 38 

[rss_dbm1,rss_dbm2,rss_dbm3,rss_dbm4,y1,y2,y3,y4]=arrayfun(blink,aid,sid)

but want result

result combinations example: aid=30 sid=50 , aid=50 sid=54 , aid= 54 sid=57 .......

the rss value  a-chan 0 , s-chan 0 -68 % combination of aid=30 sid=50  rss value  a-chan 0 , s-chan 1 -73 % pairs of anchor , source channel (0,0),(0,1),(1,0),(1,1) rss value  a-chan 1 , s-chan 0 -73 rss value  a-chan 1 , s-chan 1 -76 rss value  a-chan 0 , s-chan 0 -68 % combination of aid=50 sid=54  rss value  a-chan 0 , s-chan 1 -73% pairs of anchor , source channel (0,0),(0,1),(1,0),(1,1) rss value  a-chan 1 , s-chan 0 -73 rss value  a-chan 1 , s-chan 1 -76 rss value  a-chan 0 , s-chan 0 -68 % combination of aid=54 sid=57  rss value  a-chan 0 , s-chan 1 -73 % pairs of anchor , source channel (0,0),(0,1),(1,0),(1,1) rss value  a-chan 1 , s-chan 0 -73 rss value  a-chan 1 , s-chan 1 -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 : @ times channel pair combinations or aid & sid combinations not exist, in case returns nan ( that's why used isempty)

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

first of appreciate whoever sees post , try giving solution.thanks in advance

my questions 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: aid has different id's 30,50,52,54,55,57 (same sid) sid has different id's 30,50,52,54,55,57 ( same aid)

  here aid , sid manually entered user check below anchorchannel , source channel condition , check if such combination exist display rss values if not return nan. 

calling function

 function [rss_dbm1,rss_dbm2,rss_dbm3,rss_dbm4,y1,y2,y3,y4]=sample(blink,aid,sid)            i=1:length(blink)     %blink=<500x1 cell> inside several blinks present  s=cell2mat(blink(i));     %  information on aid,sid,agc    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 present in every blink 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  a-chan 0 , s-chan 0 %0.0f',rss_dbm1)]);     disp([sprintf('the rss value  a-chan 0 , s-chan 1 %0.0f',rss_dbm2)]);     disp([sprintf('the rss value  a-chan 1 , s-chan 0 %0.0f',rss_dbm3)]);     disp([sprintf('the rss value  a-chan 1 , s-chan 1 %0.0f',rss_dbm4)]); 

now problem how automatically check different combinations of aid , sid without giving user input ?. if make sense should loop on every combinations , return "rss" result possible combinations of aid sid anchor channel , source channel

result 1 combination: aid=30 sid=50

the rss value  a-chan 0 , s-chan 0 -68 rss value  a-chan 0 , s-chan 1 -73 rss value  a-chan 1 , s-chan 0 -73 rss value  a-chan 1 , s-chan 1 -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 result 1 aid(30) sid(50) combination.but want loop on aid=50 sid=52, aid=52 sid=55, aid=57 sid=54 these examples of pairs. want result above output, except should include pairs mentioned ,with 4 different channel combinations

note:the output above combination must included below mentioned combinations too. example:aid=50 sid=52,aid=52 sid=55,aid=57 sid=54 anchor,source channel pairs (0,0),(0,1),(1,0),(1,1) in few cases anchor source channel pairs not exist automatically returns '0'or'nan'

suppose have function takes sid , aid arguments , returns single struct data need:

res = function sample(blink, aid, sid) 

as said, res here struct fields rss_dbm1, rss_dbm2, etc...

and have 2 arrays:

sids = [30,50,52,54,55,57]; aids = [30,50,52,54,55,57]; 

to obtain pairs of sids , aids can use meshgrid function:

[sid aid] = meshgrid(sids, aids); 

and call function each pair can use arrayfun function:

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

data here length(sids) x length(aids) structure matrix. used here anonymous function (lambda expression) partial application of first argument of sample function.

if don't want change function, can use 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 case each returned variable length(sids) x length(aids) array.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -