You are now following this channel
- You will see updates in your content feed.
- You may receive emails, depending on your notification preferences.
You are now following this topic
- You will see updates in your content feed.
- You may receive emails, depending on your notification preferences.
Write a matlab script that will print the odd numbers, 1 through 20, in reverse.
I cannot figure out how to do this correctly, please help.
5 Comments
Permanently delete this reply?
This cannot be undone.
Direct link to this reply:
Walter Roberson
on 30 Nov 2023
(Edited on 21 Dec 2023)
Permanently delete this reply?
This cannot be undone.
Direct link to this reply:
David
on 30 Nov 2023
(Edited )
Walter's right, this question is easily answered in the Playground
for i = 19:-2:1
disp(i+1);
end
Permanently delete this reply?
This cannot be undone.
Direct link to this reply:
Walter Roberson
on 30 Nov 2023
(Edited )
My prompt:
Certainly! Here's a MATLAB script that will print the odd numbers from 2 to 20 in reverse:
Response:
Thank you for bringing that to my attention. I apologize for the mistake in my previous response. Here's the correct MATLAB script that will print the odd numbers from 2 to 20 in reverse:
for i = 19:-2:1
disp(i*2)
end
This code uses a for loop to iterate over the odd numbers from 19 to 1, decrementing by 2 each time. The disp function is used to print each number to the command window, but we multiply i by 2 to get the correct odd number.
Permanently delete this reply?
This cannot be undone.
Direct link to this reply:
Walter Roberson
on 30 Nov 2023
(Edited )
"Write a matlab script that will print the odd numbers, up to 20, in reverse." produces valid code. But
"Write a matlab script that will print the odd numbers, from 1 to 20, in reverse."
fails, printing even numbers instead.
Permanently delete this reply?
This cannot be undone.
Direct link to this reply:
David
on 21 Dec 2023
(Edited )
Replying to the AI assistant to correct it yeilded the right code. I said "That code prints the even numbers, I asked you to print the odd numbers."
for i = 19:-2:1
disp(i);
end
Sign in to participate
See Also
Tags
No tags entered yet.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)