Mat Lab Loop question It's not working properly. (Loops)
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示

I came up with this loop but it doesn't add up properly
x = randi(32,32,1);
for A = 1:32
disp(x(7+A))
end
or should it be like this?
x = 32;
y = rand(1,x);
z = 7:7:28;
for y(1,z) = y(1,z) + 7
fprintf('%d',y)
end
採用された回答
Rik
2019 年 8 月 25 日
Neither, you need to tackle this problem step by step.
You need to create a random vector of integers. The second example doesn't return integers, while the first does. Since the excersize didn't specify any maximum value 32 should be fine. So x=randi(32,32,1); can stay.
Now to the loop. We need to look at every 7th element. There are two basic ways we can do that. We can either have some value and multiply it by 7, but in this case the other strategy is better: use the two-colon syntax to generate list of positions to change. You did that in your second code block when creating z.
Inside the loop we need to change a value. That mean we need to overwrite the value:
%set some value to var
var=3;
%increment var by 5:
var=var+5;
The doc of the for keyword provides some help how we can continue here: look at how you can change the first example to change a specific position in a vector, and the second example for how you can enter your own list of indices.
Nothing in the assignment says anything about displaying anything, so disp and fprintf are not needed.
If you need any more help, just write a comment.
20 件のコメント
Is this one the right one but the function runs out four times
x =randi(32,32,1);
for y = 7:7:28;
x(y,1) = x(y,1) + 7
end
"but the function runs out four times"
Unfortunately the assignment does not specify the starting index: you could try starting at index 1, rather than at index 7. You might also want to change the end index to 32.
The program now runs 9 times
x =randi(32,32,1);
for y = 1:7:32;
x(y,1) = x(y,1) + 7
end
Rik
2019 年 8 月 26 日
I would expect your last code section is what your teacher expects you to write, although it is also possible they don't mean 1,8,15 etc as every seventh, but 7,14,21 etc. In that case you should be using 7:7:32.
I have one more suggestion to make it work for other sizes as well:
x =randi(32,32,1);
for y = 1:7:numel(x) %size(x,1) also works here
x(y,1) = x(y,1) + 7;
end
The code you did runs out many times. This code runs only once do you think it's correct?
x = randi(10,[1,32])
for i = [7,14,21,28]
x(i) =x(i) + 7;
end
x
Rik
2019 年 8 月 26 日
"The code you did runs out many times." What do you mean? The code you posted is equivalent to mine (except for the use of a row vector instead of a column vector). Your code has the downside of using hard-coded values, which is a bad idea.
John wick
2019 年 8 月 26 日
When I run the code it show answers 9 different time such as x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7 x= 1 2 3 4 5 6 7
@Look Mai: most likely you forgot the semi-colon ; after the code in the loop, exactly as you did in your earlier comment. Without a semi-colon MATLAB will display the result of that line on each loop iteration.
x(y) = x(y) + 7;
% ^ you need this!
As Rik wrote, you should not hard-code the indices (e.g. your vector).
The one he did and what this code both has ; but it runs differently
x = randi(10,[1,32])
for i = [7,14,21,28]
x(i) =x(i) + 7;
end
x
This one doesn't add 7 on the element 7 or I'm not sure if I try to run the code properly
for y = 1:7:numel(x)
x(y,1) = x(y,1) + 7;
end
"..but it runs differently"
The first (unfortunately written with hard-coded values) starts from index 7:
for i = [7,14,21,28]
% ^ starting index = 7
whereas the second (better code using numel) starts from index 1:
for y = 1:7:numel(x)
% ^ starting index = 1
You need to decide which is correct for your assignment.
Rik
2019 年 8 月 26 日
It uses a different definition of 7th element. It is either 7,14,21,28 or 1,8,15,22,29. The assignment isn't clear about this. The rest of this is the same. Note that x(y,1) explicitly assumes a column vector, while the x(i) syntax will also support a row vector.
I asked about the question
I want to ask is that loop of the 7th element. It is either 7,14,21,28 or 1,8,15,22,29
And this is the answer
If the first element is x(1) then the 7th element is x(7)
Rik
2019 年 8 月 26 日
Then you should use 7:7:numel(x).
So finalizing the code it should be
x =randi(32,32,1);
for y = 7:7:numel(x)
x(y,1) = x(y,1) + 7;
end
Rik
2019 年 8 月 26 日
Yes, although I would probably make it a bit more rubust by using this:
x =randi(32,32,1);
for y = 7:7:numel(x)
x(y) = x(y) + 7;
end
John wick
2019 年 8 月 26 日
More information I have been given
Make a random vector of the given length (it doesn't say row or column so your choice).
Change every 7th element (meaning 7th then 14th etc. element) by adding 7.
John wick
2019 年 8 月 26 日
Ohhh I see! thank you so much for helping me~ If I have more questions could I ask for your help?
Rik
2019 年 8 月 26 日
You're welcome. If my answer helped you, please consider marking it as accepted answer.
If your other questions are closely related to this one, feel free to post them in a comment here, otherwise it is probably better to post a separate question.
You can also have a read here to get some tips about how to find your own answers and how to write a good question. Also, your course instructor should be able to answer most of your questions. That might be a more efficient method to better understand how Matlab works.
Steven Lord
2019 年 8 月 26 日
In addition to what Rik said about asking your course instructor for help, if you haven't already done so I strongly encourage you to take the MATLAB Onramp training. It's free and it should help you develop a better understanding of how to work with MATLAB.
John wick
2019 年 8 月 26 日
I have finished it around 80% it's a fun way to learn. After I finish I will redo it again!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
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)
