필터 지우기
필터 지우기

How can i rewrite this logic for n elements?

조회 수: 1 (최근 30일)
Sumanth
Sumanth 2023년 2월 28일
댓글: Walter Roberson 2023년 2월 28일
if q(1,1) == 1
disp("q1 is selected")
D = D1;
P= P1;
elseif q(1,2) == 1
disp("q2 is selected")
D = D2;
P= P2;
elseif q(1,3) == 1
disp("q3 is selected")
D = D3;
P= P3;
end
Hello I have to re-write this logic for 10elements i.e what if q array consists of 1*10 slots unlike above i have just 3. also it is very unpredictable that when the value of q elements can be 1.
  댓글 수: 4
Torsten
Torsten 2023년 2월 28일
yes to what ?
Sumanth
Sumanth 2023년 2월 28일
exactly one of the q values = 1.

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

채택된 답변

Walter Roberson
Walter Roberson 2023년 2월 28일
편집: Walter Roberson 2023년 2월 28일
Or...
Dvals = [D1, D2, D3, D4, D5, D6, D7, D8, D9, D10];
Pvals = [P1, P2, P3, P4, P5, P6, P7, P9, P9, P10];
idx = find(q == 1, 1); %first
if ~isempty(idx)
D = Dvals(idx);
P = Pvals(idx);
else
uh oh
end
  댓글 수: 3
John D'Errico
John D'Errico 2023년 2월 28일
@Sumanth So then learn to use arrays in their many forms, possibly multidimensional arrays, or cell arrays.
Walter Roberson
Walter Roberson 2023년 2월 28일
Dvals = {D1, D2, D3, D4, D5, D6, D7, D8, D9, D10};
Pvals = {P1, P2, P3, P4, P5, P6, P7, P9, P9, P10};
idx = find(q == 1, 1); %first
if ~isempty(idx)
D = Dvals{idx};
P = Pvals{idx};
else
uh oh
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by