Mat Lab Loop question It's not working properly. (Loops)

Mat Lab question loop.jpg
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

 Accepted Answer

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 Comments

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
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
"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.
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.
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)
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
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
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.
Ohhh I see! thank you so much for helping me~ If I have more questions could I ask for your help?
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.
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.
I have finished it around 80% it's a fun way to learn. After I finish I will redo it again!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 25 Aug 2019

Commented:

on 26 Aug 2019

Community Treasure Hunt

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

Start Hunting!