Matlab - floating point operation counts (flop count)

Click For Summary

Discussion Overview

The discussion revolves around calculating the number of floating point operations (flops) in a nested loop structure written in MATLAB. Participants are exploring how to derive a summation formula for the flop count based on the given code sequence, with a focus on understanding the behavior of nested loops.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification
  • Exploratory

Main Points Raised

  • One participant seeks help in deriving a summation formula for the flop count based on the nested loops in the provided MATLAB code.
  • Another participant points out that the initial assumption of 3n flops is incorrect due to the nesting of loops and the varying limits of each loop.
  • A suggestion is made to run the code with small values of n to visualize the loop execution and understand how the variable 'a' is incremented.
  • Several participants express confusion about the concept of loops in procedural programming languages, indicating a need for foundational knowledge in programming.
  • One participant attempts to clarify how the loops operate by providing a step-by-step breakdown of the execution for n=2.
  • Another participant shares their findings for various values of n, noting a pattern in the results and expressing gratitude for the assistance received in understanding the problem.

Areas of Agreement / Disagreement

Participants generally agree on the need for a better understanding of nested loops and their execution in programming. However, there is no consensus on the correct formula for the flop count, and multiple interpretations of the loop behavior are presented.

Contextual Notes

Some participants express uncertainty about the correct implementation of the MATLAB code and the behavior of nested loops. There are also references to different programming languages that may help clarify the concepts being discussed.

Who May Find This Useful

This discussion may be useful for students learning about nested loops in programming, particularly in MATLAB, as well as those interested in understanding how to calculate operation counts in algorithmic contexts.

playboy

Homework Statement



So I was giving a problem to "calculate" the number of flops in class today..
Now, I read the examples in the textbook, and they are hard enough to understand...I am hoping that some one here can push me onto the right track :cool:



Homework Equations



find the flop count for the following sequence of commands:
a=0;
..for p=1:n
...for q=p:n
...for r=q:n
...a=a+1;
...end
...end
..end
What will be the value of a after the commands are executed ?

The Attempt at a Solution



I am not looking for a number, but a summation formula interms of n.

I am given that it has to be in the form
sum(i,n) i = n(1-n)/2
and
sum(i,n) i^2 = n(1-n)(2n-a)/6

So far, all I can see is that their must be n + n + n = 3n flops...

I have no idea how to calculate a.

Can somebody help me please?
 
Physics news on Phys.org
So far, all I can see is that their must be n + n + n = 3n flops...

That's not correct for two reasons:
1 The loops are nested inside each other
2 The loops do not all go from 1 to n.

It might help to change this into a real computer language so you can run the program, set n equal to a small number like 4 or 5, and print out p, q, r, and a in the inside loop (just after the a = a + 1) . Then you will see what's going on.
 
AlephZero said:
That's not correct for two reasons:
1 The loops are nested inside each other
2 The loops do not all go from 1 to n.

It might help to change this into a real computer language so you can run the program, set n equal to a small number like 4 or 5, and print out p, q, r, and a in the inside loop (just after the a = a + 1) . Then you will see what's going on.

Perhaps that is my problem..."the loops are nested inside each other"

I tried getting this programme to run so that I can "visualize" how it works, but I can't get MATLAB running it.

I wrote:

function [a]=bs(n)

a=0;
for p=1:n;
for q=1:n;
for r=1:n;
a=a+1;
end
end
end


and tried running [a]=bs(n) for n=3 in the commad window. I got an error.

Let's consider n = 5

p=1:5 --> p = 1 2 3 4 5
q=1:5 --> q = 1 2 3 4 5
r=1:5 --> r = 1 2 3 4 5

So i have NO idea how this loop works :eek:
 
You need to find out what a "for" loop means, in the language that is being used in these questions.

The code

for p = 1:5
(some statements)
end

doesn't mean the same as the Matlab statement

p = 1:5
 
I think my problem is understanding the entire code.

I've been pouring my thoughts out for the longest time on this question.

Could you please expalin to me how the function would compute a for n=2?

(I tried writing this function in MATLAB but failed)
 
I'll repeat what I said before: if you don't understand the concept of loops in procedural programming languages, then you need to learn about that, before you can do this type of question.

If you knew a language like C, C++, Java, Pascal, Basic, Fortran, etc, then it should be obvious what the code means, even though the notation is not exactly the same as any of those languages.

If you haven't done any courses on programming, there is a tutorial on C++ here: https://www.physicsforums.com/showthread.php?t=32703. Your "for p=1:n" means the same "for (p = 1; p <= n; p++)" in C or C++.
 
a=0;
..for p=1:n
...for q=p:n
...for r=q:n
...a=a+1;
...end
...end
..end
For n= 2.
First a is set equal to 0.
Now, p is set to 1, q is set to p= 1, r is set to q= 1
a is increased to 1

We are still in the "r" loop- now r is set to 2
a is increased to 2

Since r has reached its maximum we drop back to the q loop
q is set to 2
r is set to q= 2
a is increased to 3

Since r has reached its maximum we drop back to the q loop
Since q has reached its maximum we drop back to the p koop
p is set to 2, q is set to p= 2, r is set to q= 2
a is increased to 4

Since r has reached its maximum we drop back to the q loop
Since q has reached its maximum we drop back to the p loop
Since p has reached its maximum the program terminates.

a is equal to 4= 22 but be careful. Try n= 3 before making any hasty generaliztions!
Once you find a relation that you feel sure of, perhaps you can prove by induction on n.
 
Last edited by a moderator:
Thank you SOO much Hallsofivy! I really appreciate how you took the time to explain that to me..it reall helped me.

So I did this up until n=7 :zzz:

n=1 a=1 ----> n^2 + 0 = a
n=2 a=4 ----> n^2 + 0 = a
n=3 a=10 ---> n^2 + 1 = a
n=4 a=20 ---> n^2 + 4 = a
n=5 a=35 ---> n^2 + 10 = a
n=6 a=56 ---> n^2 + 20= a
n=7 a=84 ---> n^2 + 35 = a

I notice the pattern and I can see what has to be done.

I really appreciate your help. I know sincerely understand what is going on.
Thank you HallsofIvy!
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K