Another Noob MATLAB Question - For loop

In summary, the for loop produces the following results: Columns 1 through 9: 6, 15, 35, 77, 143, 221, 323, 437, 667Columns 10 through 18: 899, 1147, 1517, 1763, 2021, 2491, 3127, 3599, 4087Columns 19 through 24: 4757, 5183, 5767, 6557, 7387, 8633
  • #1
GreenPrint
1,196
0

Homework Statement



Use the primes function to create a list of all the primes below 100. Now use a for loop to multiply adjacent values together. For example the first 4 primes numbers are

2 3 5 7

your calculation would be

2*3 3*5 5*7

which gives

6 15 35

Homework Equations





The Attempt at a Solution


Ok I spent a very long time trying to solve this problem. I eventually solved it with the following program.

PLEASE CHECK SECOND POST
i uploaded a easier version to read, i myself couldn't stand even looking at my program below so i took a snip of it in a easier to read form

z=0;
a=length(primes(100));
for b=primes(100);
if z>1;
y=y-1;
else
y=a;
end
b=primes(100);
c(y)=b(y).*b(y-1);
z=z+1;
end
d=find(c>0);
c(d)


which produces the following results, just the way I wanted them to

ans =

Columns 1 through 9

6 15 35 77 143 221 323 437 667

Columns 10 through 18

899 1147 1517 1763 2021 2491 3127 3599 4087

Columns 19 through 24

4757 5183 5767 6557 7387 8633

my only question and concern is why in my program must I include b=primes(100); after the end statement? I thought it was enough for me to just define it once and only once after the for command? I however noticed when I did so that b would get defined as 2 for some strange reason and that's it not what primes(100) is suppose to produce.

I was hoping someone could explain this to me. Thanks in advance!
 
Last edited:
Physics news on Phys.org
  • #2
Check attachments
 

Attachments

  • Capture.JPG
    Capture.JPG
    7.9 KB · Views: 1,282
  • #3
Hm.

You don't define b as a vector before the loop, you just run through it in the for loop, so b in the loop takes on a integer value, and not a vector.

Generally: don't use b as running variable in loop, while re-assigning it a value IN the loop. Bound for troubles.


Define b before the for loop, and use a running variable i=1:1:a in the for loop.

HTH
 
  • #4
Hi Greenprint, use "code" tags to preserve formatting in posts.

That's really bad use of a for loop as outlined by Laiva above. Set up your for loop limits before the loop, something like the following.
Code:
p=primes(100);
sp=max(size(p))-1;
for k=1:sp
   pp(k)=p(k)*p(k+1);
end
 
Last edited:
  • #5
thanks I think I better understand the loop commands now =)
 

FAQ: Another Noob MATLAB Question - For loop

1. What is a for loop in MATLAB?

A for loop in MATLAB is a programming construct that allows you to repeat a set of statements for a specific number of times. It is used to automate repetitive tasks and iterate through arrays or vectors.

2. How do I write a for loop in MATLAB?

To write a for loop in MATLAB, you need to specify the starting value, the ending value, and the increment or decrement value. The basic syntax is: for i = starting value:increment/decrement value:ending value. You then need to include the statements you want to repeat between the for and end keywords.

3. Can I use a for loop to iterate through a matrix in MATLAB?

Yes, you can use a for loop to iterate through a matrix in MATLAB. You can use nested for loops to access each element in a matrix or use the colon operator to access an entire row or column at once.

4. How do I break out of a for loop in MATLAB?

You can use the break statement to break out of a for loop in MATLAB. This will immediately terminate the loop and execute the statements after the loop. You can also use the continue statement to skip the current iteration and move on to the next one.

5. Can I use a for loop to iterate through a cell array in MATLAB?

Yes, you can use a for loop to iterate through a cell array in MATLAB. However, you will need to use the curly braces notation to access the elements in the cell array. The syntax is: for i = 1:length(cell_array), element = cell_array{i}.

Similar threads

Replies
4
Views
23K
Replies
2
Views
2K
Replies
2
Views
3K
Replies
2
Views
4K
Replies
2
Views
6K
Replies
1
Views
2K
Back
Top