From: Craig on
Hi all. I'm just coming to terms with the way MATLAB processes things, and am quickly realising the benefits in the avoidance of looping wherever possible..

I have two set ups I would be ever grateful for assistance with, if possible. I know questions like this are asked all the time, I would also be delighted if someone could point me to a resource to help with this matter.

First, I have a simple thing:
for i=1:100
ord=0;
ord=randperm(66);
arp=a(ord);
bar=arp(1:x);
result=var(bar);
end

Second, I have what I am positive is a horrible set up:
for k=1:50
for h=1:50
counting=0;
for j=1:76
if R(j,3)== k
y(j,h,k) = x(j);
counting=counting+1;
elseif R(j,3) == h && h~=k
Y(j,h,k) = x(j);
counting=counting+1;
end
end
end

Sorry if I have offended some keen MATLAB'ers eyes!

Many thanks,

Craig.
From: Walter Roberson on
Craig wrote:

> First, I have a simple thing:
> for i=1:100
> ord=0;
> ord=randperm(66);
> arp=a(ord);
> bar=arp(1:x);
> result=var(bar);
> end

The result is the same as only doing a single iteration, unless a or var
are functions that have side effects.

If result should have been indexed, then in order to know whether you
could get any meaningful improvement, we need to know whether var is a
function or an array, and whether it always returns the same size of result.

Without knowing that we can make some minor optimizations:

nruns = 100;
result = cell(nruns, 1);
La = length(a);
for i = 1 : nruns
arp = a(randperm(La));
result{i} = var(arp(1:x));
end

If var is a variable, then we can improve on this. If var is a function,
the function would have to be able to accept a matrix of values in order
to get much improvement.
From: Craig on
Thank you for your reply, Walter!

>
> The result is the same as only doing a single iteration, unless a or var
> are functions that have side effects.
>
> If result should have been indexed, then in order to know whether you
> could get any meaningful improvement, we need to know whether var is a
> function or an array, and whether it always returns the same size of result.
>
'a' is simply a vector that is re-arranged according to the randperm. 'var' is the MATLAB function to calculate the 'variance'.

I have missed out a part of the above code. It should read:

result(i) = var(bar);

So that 'result' ends up being a vector storing a value of variance for each pass of the loop.

I believe 'var' does work with matrices. Is it then possible in some fashion to set up a matrix with all of the combinations of 'a' that i want to look at and calculate the variance of each of them in 'one go' as it were?

I know it is not relevant but i am calculating the relevance of chemical data. As a chemist, mathematics and computing has not come naturally to me so far! I thank you for your help.


Craig.
From: Craig on

> Second, I have what I am positive is a horrible set up:
> for k=1:50
> for h=1:50
> counting=0;
> for j=1:76
> if R(j,3)== k
> y(j,h,k) = x(j);
> counting=counting+1;
> elseif R(j,3) == h && h~=k
> Y(j,h,k) = x(j);
> counting=counting+1;
> end
> end
> end
>

An obvious inefficiency i want to fix for this part is that when e.g. the k=1 and h=2 satisfy the IF loop, the combination of k=2 and h=1 also results in the same calculation - so the set up is doing each calculation twice. If anyone has any ideas.

thanks.

Craig.
From: Bas on
On Jul 12, 4:01 am, "Craig " <physicsmathwo...(a)gmail.com> wrote:
> for k=1:50
>     for h=1:50
>         counting=0;
>         for j=1:76
>             if R(j,3)== k
>                 y(j,h,k) = x(j);
>                 counting=counting+1;
>             elseif R(j,3) == h && h~=k
>                 Y(j,h,k) = x(j);
>                 counting=counting+1;
>             end
>         end
> end

Not exactly sure what you want to do, but you might remove the inner
loop by using logical arrays or index arrays. Note that you reset
counting each loop (so only the value of the last loop remains) and
that you use both y and Y (matlab is case sensitive), not sure if that
is a typo or intended. Untested code:

for k=1:50
for h=1:50
counting=0;
ind = find(R(:,3) == k);
y(ind,h,k) = x(ind);
counting = counting + length(ind);
if h ~= k
ind = find(R(:,3) ~= k && R(:,3) == h);
Y(ind,h,k) = x(ind);
counting = counting + length(ind);
end
end
end


Hope that helps,
Bas