Preallocated array A using a for loop
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Consider an iterative series 1, (-1)/2, 1/3, (-1)/4, 1/5,…(-1)^(k-1)/k. The first 5 terms for k=1:5 are written here. Create the first 100 and store them all in a preallocated array A using a for loop.
Please help with this question. Thanks in advance!
採用された回答
madhan ravi
2019 年 9 月 15 日
https://in.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html - you will see how to store values in each iteration through preallocation.
8 件のコメント
timetry2
2019 年 9 月 15 日
I did look through this already, however, I am still confused on how to exactly code it to do a for loop. Could you show me how the code itself would look like.
Thank you
madhan ravi
2019 年 9 月 15 日
編集済み: madhan ravi
2019 年 9 月 15 日
Just come up with the code , doesn’t matter if it’s right or wrong. We’ll correct the code once it’s constructed.
timetry2
2019 年 9 月 15 日
編集済み: madhan ravi
2019 年 9 月 15 日
Im unfamiliar with tic toc functions but here is the code I came up with:
tic
x = zeros (1,100);
for k = 1:5
x(k)=(-1)^(k-1)/k
end
toc
Please let me know what to fix and thanks for the help!
madhan ravi
2019 年 9 月 15 日
Ignore tic toc , concentrate the length of k you are only calculating until 5 but the task asks you to do it till 100.
timetry2
2019 年 9 月 15 日
編集済み: Image Analyst
2019 年 9 月 15 日
So, would I just need to change k = 1:100 instead of k = 1:5?
So, the line of code would look like the following:
tic
x = zeros (1,100);
for k = 1:100
x(k) = (-1)^(k-1)/k
end
toc
Image Analyst
2019 年 9 月 15 日
Correct.
timetry2
2019 年 9 月 15 日
One last thing, what are the tic toc functions doing then? I've only learned about for and while loops. And, I was curious how this function works without incorporating either. Thank you so much for the help.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
