Re: How can i simplify this messy for loop???!!!





Geoff Halstead wrote:
> i'm trying to cut the processing time of a larger code by predefining
> some of the elements in the matrix, H below. I basically need to set
> certain columns of my H matrix to zero. The code needs to be general
> so that N can be varied easily. At the moment this for loop is taking
> more time than my original drawn out code. Is there any way of
> considerably reducing the time it takes to set the desired columns to
> zero?
>
> N = 30;
> H_1 = ones(N^2);
> H_1 = H_1*2;
>
> for col_1 = 1:1+N
> for col_2 = N^2-N:N^2
> for col_3 = 2*N:N:(N-1)*N
> for col_4 = (2*N)+1:N:((N-1)*N)+1

Why are these nested? You aren't picking
a different value of col_2 for each value
of col_1, etc.

You have four completely independent sets of
columns you are trying to zero. If you were
doing it with for loops, it would be four
independent for loops, not one nested set.

> H_1(:,col_1) = 0;

Here's a clue as to inappropriate nesting. This
expression doesn't depend on col_2, col_3, or
col_4. It shouldn't be inside those loops. It
only needs to happen once for each value of col_1.

> H_1(:,col_2) = 0;
> H_1(:,col_3) = 0;

And of course the same applies here.

> H_1(:,col_4) = 0;
> end
> end
> end
> end

You don't need FOR loops at all, but I'm going to
mess with your loops to illustrate my point about
nesting.

for col_1 = 1:1+N
H_1(:, col_1) = 0;
% When writing nested stuff like this, you should have
% your eye out for statements which don't depend on the
% inner variables. This statement only should be executed
% once for each value of col_1 (you are executing it thousands
% of times), so it belongs up at this level.
for col_2 = N^2-N:N^2
H_1(:,col_2) = 0;
% Same thing applies here.
for col_3 = 2*N:N:(N-1)*N
H_1(:,col_3) = 0;
% And here.
for col_4 = (2*N)+1:N:((N-1)*N)+1
H_1(:,col_4) = 0;
end
end
end
end

But in your case, that's wrong too. You don't want to
go through the col_2 loop again and again each time for
every separate value of col_1. There's no dependence
on col_1. So this structure is more appropriate:

for col_1 = 1:1+N
H_1(:, col_1) = 0;
end

for col_2 = N^2-N:N^2
H_1(:,col_2) = 0;
end

for col_3 = 2*N:N:(N-1)*N
H_1(:,col_3) = 0;
end

for col_4 = (2*N)+1:N:((N-1)*N)+1
H_1(:,col_4) = 0;
end

And actually, you don't need FOR loops at all.

col_1 = 1:1+N
H_1(:,col_1) = 0;

col_2 = N^2-N:N^2
H_1(:,col_2) = 0;

col_3 = 2*N:N:(N-1)*N
H_1(:,col_3) = 0;

col_4 = (2*N)+1:N:((N-1)*N)+1
H_1(:,col_4) = 0;

I just let each variable take on ALL of its values at
once, as a vector, rather than be an argument to a FOR
loop. I left it as col_1, col_2, etc. for readability.
But if you don't care much about readability:

col_1 = 1:1+N
col_2 = N^2-N:N^2
col_3 = 2*N:N:(N-1)*N
col_4 = (2*N)+1:N:((N-1)*N)+1
H_1(:,[col_1 col_2 col_3 col_4]) = 0;

or even:

H_1(:,[1:1+N, N^2-N:N^2, 2*N:N:(N-1)*N, (2*N)+1:N:((N-1)*N)+1]) = 0;

- Randy

.



Relevant Pages

  • Re: Fortran 2003/2008 features and compiler poll
    ... There are lots of concrete good reasons for having empty contains. ... special-case coding for no particular reason. ... I suppose that one could special-case the zero possibility, ... I suppose you also dislike zero-trip DO loops and zero-sized arrays? ...
    (comp.lang.fortran)
  • Re: Ground Busses
    ... it is all very well connecting all grounded leads to a single point but if you then connect that by a single wire to the zero volts point of the power supply you have immediately introduced a long no zero common ground bus. ... of the current loops in the circuit, from power supply out to the signal ... then separate out these loops so they meet at (to the greatest extent ... The problem I see with that is that all output currents must flow from the power supply, through the load and back to the PSU 0V but they share a common ground with the inputs. ...
    (rec.audio.tubes)
  • Re: Ground Busses
    ... it is all very well connecting all grounded leads to a single ... point but if you then connect that by a single wire to the zero volts ... So a star ground really only works if the star point IS ... of the current loops in the circuit, from power supply out to the signal ...
    (rec.audio.tubes)
  • Re: Ground Busses
    ... point but if you then connect that by a single wire to the zero volts ... So a star ground really only works if the star point IS ... of the current loops in the circuit, from power supply out to the signal ... then separate out these loops so they meet at (to the greatest extent ...
    (rec.audio.tubes)
  • Re: Random number help
    ... r = input('Enter the number for the power r greater than zero in the ... for loops produces a Matrix of random numbers ... iteration of main for loop ...
    (comp.soft-sys.matlab)