Implementing a routing table in Matlab

조회 수: 2 (최근 30일)
Aftab Ahmed Khan
Aftab Ahmed Khan 2015년 3월 19일
답변: shivangi mahajan 2019년 11월 11일
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
Aftab Ahmed Khan
Aftab Ahmed Khan 2015년 3월 19일
Yes exactly, thats how i feel about it as well. Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (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