Implementing a routing table in Matlab

3 vues (au cours des 30 derniers jours)
Aftab Ahmed Khan
Aftab Ahmed Khan le 19 Mar 2015
Hi, This is my routing table which i have made. I want to ask that is there any better way in which i can implement this. Thank you.
if any(idabs==[1,2,5,6])
hbsoption1=1; hbsoption2=5; hbsoption3=4; hbsoption4=6; hbsoption5=2; hbsoption6=3;
elseif any(idabs==([3,4,7,8]))
hbsoption1=2; hbsoption2=6; hbsoption3=3; hbsoption4=5; hbsoption5=1; hbsoption6=4;
elseif any(idabs==([9,10,13,14]))
hbsoption1=4; hbsoption2=8; hbsoption3=1; hbsoption4=7; hbsoption5=3; hbsoption6=2;
else
hbsoption1=3; hbsoption2=7; hbsoption3=2; hbsoption4=8; hbsoption5=4; hbsoption6=1;
end

Réponse acceptée

Guillaume
Guillaume le 19 Mar 2015
One way:
ids = [1 2 5 6
3 4 7 8
9 10 13 14];
options = [1 5 4 6 2 3
2 6 3 5 1 4
4 8 1 7 3 2
3 7 2 8 4 1];
idabs = randi(20) %for demo
optionrow = find([any(idabs == ids, 2); 1], 1);
hbsoption = options(optionrow, :)
It's not a good idea to create numbered variables. A vector is a lot more useful.
  1 commentaire
Aftab Ahmed Khan
Aftab Ahmed Khan le 19 Mar 2015
Yes exactly, thats how i feel about it as well. Thank you so much.

Connectez-vous pour commenter.

Plus de réponses (1)

shivangi  mahajan
shivangi mahajan le 11 Nov 2019
if any(idabs==[1,2,5,6])
hbsoption1=1; hbsoption2=5; hbsoption3=4; hbsoption4=6; hbsoption5=2; hbsoption6=3;
elseif any(idabs==([3,4,7,8]))
hbsoption1=2; hbsoption2=6; hbsoption3=3; hbsoption4=5; hbsoption5=1; hbsoption6=4;
elseif any(idabs==([9,10,13,14]))
hbsoption1=4; hbsoption2=8; hbsoption3=1; hbsoption4=7; hbsoption5=3; hbsoption6=2;
else
hbsoption1=3; hbsoption2=7; hbsoption3=2; hbsoption4=8; hbsoption5=4; hbsoption6=1;
end

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by