필터 지우기
필터 지우기

How to add a string output to this function?

조회 수: 2 (최근 30일)
Lorenz
Lorenz 2013년 6월 10일
Is it possible to add an ouptput, which says what operation made the function, to this following function?
function [op] = operation( A,B )
X=[A+B; A-B; A/B; A*B; B-A; B/A;]
Y=X(randperm(6),:)
if Y(1,1)>0 && Y(1,1)==int16(Y(1,1))
op=Y(1,1);
else
operation(A,B)
end
end
For example,
EDU>> operation(4,2)
ans=
6
The output is 6, wa all knows that the function did 4+2=6, but how to add the output '+' . For example output:
op = 6 s = +

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 6월 10일
function [op,op1] = operation( A,B )
X=[A+B; A-B; A/B; A*B; B-A; B/A;];
o={'plus','minus','div','prod','iminus','idiv'}
ii=randperm(6)
op1=o{ii(1)};
Y=X(ii,:);
if Y(1,1)>0 && Y(1,1)==int16(Y(1,1))
op=Y(1,1);
else
[op,op1]=operation(A,B);
end
end

추가 답변 (1개)

David Sanchez
David Sanchez 2013년 6월 10일
function [op,s] = operation( A,B )
% your code here
s = '+'; % or whatever you want to be the string
Use a switch/case to choose among your options

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by