2

Similar to this question:

Can a Matlab PARFOR loop be programmatically switched on/off?

I'd like to know if I can use something similar for regular for-loops. Unfortunately I don't actually have a working copy of Matlab at the moment so I can't test this in my own code!

if (flag)
  for i = 1:n
else
  parfor i = 1:n
end

  % Do loop tasks.

  end

EDIT -

(Upon further reflection, I've put greater detail into my question)

This is what I currently have:

for i = 1:numel(Ffi)
    Ff = Ffi(i)

    for j = 1:numel(RelToli)
        RelTol = RelToli(j)

        for k = 1:numel(ki)
            k=ki(k)

                 % solve

        end
    end
end    

I want to change it so that the user can choose a single value for Ff/RelTol/k directly (via GUI/requested input) or, if not specified by the user, to use all values in a pre-defined array (Ffi/RelToli/ki respectively) via a for-loop.

Community
  • 1
  • 1
Tosmai
  • 60
  • 8

2 Answers2

2

I'm pretty sure that that will not work. But this will:

for i = 1:(flag*n + ~flag)

So looking at (flag*n + ~flag), if flag is true it will equal n (i.e. 1*n + 0) and if flag is false is will equal 1 (0*n + 1)

EDIT

For your updated question:

Set a flag if the user enters a value and then

if flag
    F = Ff; %//i.e. user input scalar
else
    F = Ffi; %//i.e. Whole vector
end

now:

for Ff = F
Dan
  • 45,079
  • 17
  • 88
  • 157
  • Ok, I've looked at the code I currently have (in Notepad) and I've thought more closely about how I actually want to change it. I'll edit the original question. – Tosmai Mar 05 '14 at 13:36
  • oooo perfect. thanks a lot, this looks like just what I wanted :) – Tosmai Mar 05 '14 at 13:54
1

What about this?

if flag
    limit = n;
else
    limit = 1;
end

for i = 1:limit
    ...
end
Sam Roberts
  • 23,951
  • 1
  • 40
  • 64