필터 지우기
필터 지우기

Displaying least assigned value

조회 수: 2 (최근 30일)
Taimur Saleem
Taimur Saleem 2013년 10월 16일
댓글: Taimur Saleem 2013년 10월 17일
I need to know of a function that can choose the least value in the difference command and display its variable name.
My code runs something like:
diff=mod((var1-var2),3)
if diff==2
disp(%the name of the variable which has the lower value)
end

채택된 답변

Jan
Jan 2013년 10월 17일
It is a bad programming practice to let the names of the variables carry information, which is used for the processing. Mixing the program and the processing values violates the idea of an abstract algorithm.
Therefore I recommend:
V(1).value = 6;
V(1).name = 'variable1';
V(2).value = 1;
V(2).name = 'variable2';
Value = [V.value];
if mod(Value(1) - Value(2), 3) == 2
[dummy, index] = min(Value);
fprintf('%s is the smaller variable', V(index).name);
end
  댓글 수: 1
Taimur Saleem
Taimur Saleem 2013년 10월 17일
Well I guess you answered the question and yes I am aware that it is a bad programming practice but in my case it makes the code shorter and I wouldn't have to write 15 if commands for every possible permutation of the outcome.

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

추가 답변 (1개)

sixwwwwww
sixwwwwww 2013년 10월 16일
Dear Taimur Saleem,
Here is the code which do it:
var1 = 6;
var2 = 1;
difference = mod((var1 - var2),3);
if difference == 2
if var1 < var2
disp('var1 is smaller variable')
else
disp('var2 is smaller variable')
end
end
I hope it helps
  댓글 수: 1
Taimur Saleem
Taimur Saleem 2013년 10월 17일
Well it did clarify something but I wanted a code which would display the variable name which I assigned to the value since I have 2 variables which fit the criteria for diff==2. I want to display the name of whichever of them has the lower value.

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

카테고리

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