필터 지우기
필터 지우기

Execute a group of 'if' statements in random order

조회 수: 2 (최근 30일)
MANISH KUMAR
MANISH KUMAR 2016년 7월 12일
답변: Walter Roberson 2016년 7월 12일
Suppose if we have few 'if' statements and I want these to be executed in random order. For example, here are given five 'if' statements one for each row of a matrix. In random execution, any 'if' statement may execute first and similarly rest functions.
X is a random matrix of order 5*10
if any(X(1,:))
X([4 5],:)=0;
end
if any(X(2,:))
X(4,:)=0;
end
if any(X(3,:))
X(5,:)=0;
end
if any(X(4,:))
X(2,:)=0;
end
if any(X(5,:))
X([1 3],:)=0;
end

채택된 답변

Walter Roberson
Walter Roberson 2016년 7월 12일
order_to_use = randperm(NumberOfConditions);
for idx = 1 : length(order_to_use)
switch order_to_use(idx)
case 1: if any(X(1,:)); X([4 5],:)=0; end
case 2: if any(X(2,:)); X(4,:)=0; end
...
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by