Re: How can i simplify this messy for loop???!!!
- From: "Randy Poe" <poespam-trap@xxxxxxxxx>
- Date: 25 Jul 2005 09:13:06 -0700
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
.
- Follow-Ups:
- Re: How can i simplify this messy for loop???!!!
- From: Geoff Halstead
- Re: How can i simplify this messy for loop???!!!
- References:
- How can i simplify this messy for loop???!!!
- From: Geoff Halstead
- How can i simplify this messy for loop???!!!
- Prev by Date: 3d volume. Vertices vs Squares.
- Next by Date: Re: Loading an object after changing its structure
- Previous by thread: Re: How can i simplify this messy for loop???!!!
- Next by thread: Re: How can i simplify this messy for loop???!!!
- Index(es):
Relevant Pages
|