Implementing a routing table in Matlab

8 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2015-3-19
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 个)

shivangi  mahajan
shivangi mahajan 2019-11-11
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

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by