I have to create this program which flips two values and both are inputted by the user. I have to flip the value in a single line of code in the for loop. I have figured it using three lines of code but one line in the for loop is confusing me.
user1=input('')
user2=input('')
for total=1:5
.........
end
Basic Output
user1=1
userr2=2
user1=2
userr2=1
user1=1
userr2=2

댓글 수: 2

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 12일
Why in a for loop?
Rooy
Rooy 2013년 10월 12일
It is required unless there is another way

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

 채택된 답변

Jos (10584)
Jos (10584) 2013년 10월 12일

0 개 추천

Like other, I totally fail to see why you need a for loop ... Anyways, this will work
for k=1,
[b,a] = deal(a,b)
end

추가 답변 (2개)

Jos (10584)
Jos (10584) 2013년 10월 12일

1 개 추천

Hint:
A(1) = 1
A(2) = 2
A([2 1])

댓글 수: 1

Rooy
Rooy 2013년 10월 12일
I kind of need a single line of code in a for loop that does the trick. Anyway really appreciate your help. Thank you

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

sixwwwwww
sixwwwwww 2013년 10월 12일

0 개 추천

Dear Rooy, here is the desired code without for loop:
a = input('Input first value: ');
b = input('Input second value: ');
c = a;
a = b;
b = c;
disp(strcat('First value is : ', num2str(a)))
disp(strcat('Second value is: ', num2str(b)))

댓글 수: 1

Rooy
Rooy 2013년 10월 12일
Thank you , that is exactly what I did but I have to do it in one line of code

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2013년 10월 12일

댓글:

2013년 10월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by