From: Sepehr Sadighpour on
Hi,

This is a multi-dimensional puzzle that I suspect has a more elegant solution than more and more for-loops. I need advice on various parts of my code, which I hope to optimize with your help. Please feel free to suggest any faster or cleaner ways of doing this problem that you can think of. Where I am stuck is the innermost loop in the code below, wherein I have asked my primary question about taking the next step.

THE DATA:
99,000 strings of pseudo-random numbers. Length of each string =100. The breakdown: 1000 strings for each Hurst value; 99 Hurst values (from .01 to .99). Hence, 99000 strings. [Generated by function wfbm.]

THE GOAL:
Calculate the convex hull for each of these strings. Draw vectors from one point on the hull to the next, then count how many of these hull vectors are parallel or anti-parallel to each other for each sample at each Hurst value.

THE CODE:
%We generate the 4-dimensional matrix HULL_VECTORS below (see innermost loop). It contains on each row a normalized vector [x1,y1; x2,y2; ...] composing the convex hull of each string. It is 1000 deep, and has 99 instances in the 4th dimension, 1 for each Hurst value from .01 to .99. %

STRING_LENGTH=100;
POPULATION_SIZE = 1000; %number of strings of length STRING_LENGTH for each Hurst exponent value%
HURST_INCREMENT = 0.01;

MIN_H = HURST_INCREMENT;
MAX_H = 1-HURST_INCREMENT;
H = MIN_H:HURST_INCREMENT:MAX_H;
NUM_H_VALUES = length(H);

%preallocate a matrix to be populated by wfbm with pseudo-random numbers%
FBM_SIM = zeros(POPULATION_SIZE,STRING_LENGTH,NUM_H_VALUES);
%preallocate a matrix to contain the number of hull points for each string%
NUM_HULL_POINTS = zeros(POPULATION_SIZE,NUM_H_VALUES);

randn('state',1);

for ih = 1:NUM_H_VALUES;
for n = 1:POPULATION_SIZE;
%fractional brownian motion simulation%
FBM_SIM(n,:,ih) = wfbm(H(ih),STRING_LENGTH);
HULL_INDICES = convhulln([0:STRING_LENGTH-1;FBM_SIM(n,:,ih)]');
NUM_HULL_POINTS(n,ih) = length(HULL_INDICES);
xVals = sort(HULL_INDICES(:,1))';
yVals = FBM_SIM(n,xVals,ih);
HULL_COORDS=[xVals;yVals]';
%Calculate Hull vectors & normalize%
for j=1:NUM_HULL_POINTS(n,ih)-1;
HULL_VECTORS (j,:,n,ih) = [xVals(j+1)-xVals(j), yVals(j+1)-yVals(j)]/sqrt((xVals(j+1) - xVals(j))^2 + (yVals(j+1) - yVals(j))^2);
%%%%%%%%%%%%%%%%%%%
QUESTION:
Now I need to test whether any of the vectors on each hull are parallel (+/- 0.5 degrees) or anti-parallel (180 degrees +/- 0.5) to each other, perhaps by first getting theta: THETA(k,m,n,ih)=arccos(dot(HULL_VECTORS(k,:,1,1),HULL_VECTORS(m,:,1,1)))
where k and m go from 1:NUM_HULL_POINTS(n,ih)?
-->I basically want the dot product of each vector with every vector that comes after it in the hull.
Any advice on how to do this effectively?
Any other functions for testing whether vectors are coplanar than arccos(dot(vector1,vector2))?
%%%%%%%%%%%%%%%%%%%

end
end
end

-------

Hope I've explained my problem thoroughly. Thanks in advance,
Sepehr
From: Bruno Luong on
"Sepehr Sadighpour" <sepehr125(a)gmail.com> wrote in message <i3jalf$geh$1(a)fred.mathworks.com>...
> Hi,
>
> This is a multi-dimensional puzzle that I suspect has a more elegant solution than more and more for-loops. I need advice on various parts of my code, which I hope to optimize with your help. Please feel free to suggest any faster or cleaner ways of doing this problem that you can think of. Where I am stuck is the innermost loop in the code below, wherein I have asked my primary question about taking the next step.
>
> THE DATA:
> 99,000 strings of pseudo-random numbers. Length of each string =100. The breakdown: 1000 strings for each Hurst value; 99 Hurst values (from .01 to .99). Hence, 99000 strings. [Generated by function wfbm.]
>
> THE GOAL:
> Calculate the convex hull for each of these strings. Draw vectors from one point on the hull to the next, then count how many of these hull vectors are parallel or anti-parallel to each other for each sample at each Hurst value.
>
> THE CODE:
> %We generate the 4-dimensional matrix HULL_VECTORS below (see innermost loop). It contains on each row a normalized vector [x1,y1; x2,y2; ...] composing the convex hull of each string. It is 1000 deep, and has 99 instances in the 4th dimension, 1 for each Hurst value from .01 to .99. %
>
> STRING_LENGTH=100;
> POPULATION_SIZE = 1000; %number of strings of length STRING_LENGTH for each Hurst exponent value%
> HURST_INCREMENT = 0.01;
>
> MIN_H = HURST_INCREMENT;
> MAX_H = 1-HURST_INCREMENT;
> H = MIN_H:HURST_INCREMENT:MAX_H;
> NUM_H_VALUES = length(H);
>
> %preallocate a matrix to be populated by wfbm with pseudo-random numbers%
> FBM_SIM = zeros(POPULATION_SIZE,STRING_LENGTH,NUM_H_VALUES);
> %preallocate a matrix to contain the number of hull points for each string%
> NUM_HULL_POINTS = zeros(POPULATION_SIZE,NUM_H_VALUES);
>
> randn('state',1);
>
> for ih = 1:NUM_H_VALUES;
> for n = 1:POPULATION_SIZE;
> %fractional brownian motion simulation%
> FBM_SIM(n,:,ih) = wfbm(H(ih),STRING_LENGTH);
> HULL_INDICES = convhulln([0:STRING_LENGTH-1;FBM_SIM(n,:,ih)]');
> NUM_HULL_POINTS(n,ih) = length(HULL_INDICES);
> xVals = sort(HULL_INDICES(:,1))';
> yVals = FBM_SIM(n,xVals,ih);
> HULL_COORDS=[xVals;yVals]';
> %Calculate Hull vectors & normalize%
> for j=1:NUM_HULL_POINTS(n,ih)-1;
> HULL_VECTORS (j,:,n,ih) = [xVals(j+1)-xVals(j), yVals(j+1)-yVals(j)]/sqrt((xVals(j+1) - xVals(j))^2 + (yVals(j+1) - yVals(j))^2);
> %%%%%%%%%%%%%%%%%%%
> QUESTION:
> Now I need to test whether any of the vectors on each hull are parallel (+/- 0.5 degrees) or anti-parallel (180 degrees +/- 0.5) to each other, perhaps by first getting theta: THETA(k,m,n,ih)=arccos(dot(HULL_VECTORS(k,:,1,1),HULL_VECTORS(m,:,1,1)))
> where k and m go from 1:NUM_HULL_POINTS(n,ih)?
> -->I basically want the dot product of each vector with every vector that comes after it in the hull.
> Any advice on how to do this effectively?

Work with matrix.

Compute all the "hull vectors", assume there are m vectors (btw for your question, it's not relevant from a hull or not, and whereas the set corresponds to hurst, and whereas generated by wfbm(). So next time when ask the question please leave those irrelevant detail on the side you'll get better chance that someone goes through the post and sort out the detail)

Gather the set of m-vectors in V :=matrix of size (m x 2), the cross dot product of vectors can be computed by W = V*V' an (m x m) matrix. I let you the pleasure to find the threshold so that when abs(W(i,j)) > threshold, then the angle of the vectors #i and #j are with in 0.5 degree to each other.

Bruno
From: Sepehr Sadighpour on
Bruno, thanks for a good suggestion. This one I will keep short and sweet.
Can you help me save the output of the cross dot product in an array? My attempt is below, and it doesn't work.

for ih = 1:NUM_H_VALUES;
for n = 1:POPULATION_SIZE;
DOTS(:,:,n,ih) = HULL_VECTORS(:,:,n,ih) * HULL_VECTORS(:,:,n,ih)';
end
end

With thanks,
Sepehr

"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i3jd81$pk3$1(a)fred.mathworks.com>...
> "Sepehr Sadighpour" <sepehr125(a)gmail.com> wrote in message <i3jalf$geh$1(a)fred.mathworks.com>...
> > Hi,
> >
> > This is a multi-dimensional puzzle that I suspect has a more elegant solution than more and more for-loops. I need advice on various parts of my code, which I hope to optimize with your help. Please feel free to suggest any faster or cleaner ways of doing this problem that you can think of. Where I am stuck is the innermost loop in the code below, wherein I have asked my primary question about taking the next step.
> >
> > THE DATA:
> > 99,000 strings of pseudo-random numbers. Length of each string =100. The breakdown: 1000 strings for each Hurst value; 99 Hurst values (from .01 to .99). Hence, 99000 strings. [Generated by function wfbm.]
> >
> > THE GOAL:
> > Calculate the convex hull for each of these strings. Draw vectors from one point on the hull to the next, then count how many of these hull vectors are parallel or anti-parallel to each other for each sample at each Hurst value.
> >
> > THE CODE:
> > %We generate the 4-dimensional matrix HULL_VECTORS below (see innermost loop). It contains on each row a normalized vector [x1,y1; x2,y2; ...] composing the convex hull of each string. It is 1000 deep, and has 99 instances in the 4th dimension, 1 for each Hurst value from .01 to .99. %
> >
> > STRING_LENGTH=100;
> > POPULATION_SIZE = 1000; %number of strings of length STRING_LENGTH for each Hurst exponent value%
> > HURST_INCREMENT = 0.01;
> >
> > MIN_H = HURST_INCREMENT;
> > MAX_H = 1-HURST_INCREMENT;
> > H = MIN_H:HURST_INCREMENT:MAX_H;
> > NUM_H_VALUES = length(H);
> >
> > %preallocate a matrix to be populated by wfbm with pseudo-random numbers%
> > FBM_SIM = zeros(POPULATION_SIZE,STRING_LENGTH,NUM_H_VALUES);
> > %preallocate a matrix to contain the number of hull points for each string%
> > NUM_HULL_POINTS = zeros(POPULATION_SIZE,NUM_H_VALUES);
> >
> > randn('state',1);
> >
> > for ih = 1:NUM_H_VALUES;
> > for n = 1:POPULATION_SIZE;
> > %fractional brownian motion simulation%
> > FBM_SIM(n,:,ih) = wfbm(H(ih),STRING_LENGTH);
> > HULL_INDICES = convhulln([0:STRING_LENGTH-1;FBM_SIM(n,:,ih)]');
> > NUM_HULL_POINTS(n,ih) = length(HULL_INDICES);
> > xVals = sort(HULL_INDICES(:,1))';
> > yVals = FBM_SIM(n,xVals,ih);
> > HULL_COORDS=[xVals;yVals]';
> > %Calculate Hull vectors & normalize%
> > for j=1:NUM_HULL_POINTS(n,ih)-1;
> > HULL_VECTORS (j,:,n,ih) = [xVals(j+1)-xVals(j), yVals(j+1)-yVals(j)]/sqrt((xVals(j+1) - xVals(j))^2 + (yVals(j+1) - yVals(j))^2);
> > %%%%%%%%%%%%%%%%%%%
> > QUESTION:
> > Now I need to test whether any of the vectors on each hull are parallel (+/- 0.5 degrees) or anti-parallel (180 degrees +/- 0.5) to each other, perhaps by first getting theta: THETA(k,m,n,ih)=arccos(dot(HULL_VECTORS(k,:,1,1),HULL_VECTORS(m,:,1,1)))
> > where k and m go from 1:NUM_HULL_POINTS(n,ih)?
> > -->I basically want the dot product of each vector with every vector that comes after it in the hull.
> > Any advice on how to do this effectively?
>
> Work with matrix.
>
> Compute all the "hull vectors", assume there are m vectors (btw for your question, it's not relevant from a hull or not, and whereas the set corresponds to hurst, and whereas generated by wfbm(). So next time when ask the question please leave those irrelevant detail on the side you'll get better chance that someone goes through the post and sort out the detail)
>
> Gather the set of m-vectors in V :=matrix of size (m x 2), the cross dot product of vectors can be computed by W = V*V' an (m x m) matrix. I let you the pleasure to find the threshold so that when abs(W(i,j)) > threshold, then the angle of the vectors #i and #j are with in 0.5 degree to each other.
>
> Bruno
From: Bruno Luong on
"Sepehr Sadighpour" <sepehr125(a)gmail.com> wrote in message <i3lm4i$9ec$1(a)fred.mathworks.com>...
> Bruno, thanks for a good suggestion. This one I will keep short and sweet.
> Can you help me save the output of the cross dot product in an array? My attempt is below, and it doesn't work.
>
> for ih = 1:NUM_H_VALUES;
> for n = 1:POPULATION_SIZE;
> DOTS(:,:,n,ih) = HULL_VECTORS(:,:,n,ih) * HULL_VECTORS(:,:,n,ih)';
> end
> end

As I understand the DOTS have different sizes for diffetent ih and n. You probably need to strore them in CELL.

DOTS = cell( NUM_H_VALUES, POPULATION_SIZE);
for ih = 1:NUM_H_VALUES;
for n = 1:POPULATION_SIZE;
DOTS{n,ih} = HULL_VECTORS(:,:,n,ih) * HULL_VECTORS(:,:,n,ih)';
end
end

Bruno
From: Sepehr Sadighpour on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i3lp61$lqa$1(a)fred.mathworks.com>...
> "Sepehr Sadighpour" <sepehr125(a)gmail.com> wrote in message <i3lm4i$9ec$1(a)fred.mathworks.com>...
> > Bruno, thanks for a good suggestion. This one I will keep short and sweet.
> > Can you help me save the output of the cross dot product in an array? My attempt is below, and it doesn't work.
> >
> > for ih = 1:NUM_H_VALUES;
> > for n = 1:POPULATION_SIZE;
> > DOTS(:,:,n,ih) = HULL_VECTORS(:,:,n,ih) * HULL_VECTORS(:,:,n,ih)';
> > end
> > end
>
> As I understand the DOTS have different sizes for diffetent ih and n. You probably need to strore them in CELL.
>
> DOTS = cell( NUM_H_VALUES, POPULATION_SIZE);
> for ih = 1:NUM_H_VALUES;
> for n = 1:POPULATION_SIZE;
> DOTS{n,ih} = HULL_VECTORS(:,:,n,ih) * HULL_VECTORS(:,:,n,ih)';
> end
> end
>
> Bruno

Currently DOTS returns a 99x99 cell that is completely empty. Here's my code:

for ih = 1:NUM_H_VALUES;
for n = 1:POPULATION_SIZE;
FBM_SIM(n,:,ih) = wfbm(H(ih),STRING_LENGTH);
HULL_INDICES = convhulln([0:STRING_LENGTH-1;FBM_SIM(n,:,ih)]');
NUM_HULL_POINTS(n,ih) = length(HULL_INDICES);
xVals = sort(HULL_INDICES(:,1))';
yVals = FBM_SIM(n,xVals,ih);
HULL_COORDS=[xVals;yVals]';

for j=1:NUM_HULL_POINTS(n,ih)-1;
HULL_VECTORS (j,:,n,ih) = [xVals(j+1)-xVals(j), yVals(j+1)-yVals(j)]/sqrt((xVals(j+1) - xVals(j))^2 + (yVals(j+1) - yVals(j))^2);
end

DOTS = cell(NUM_H_VALUES, POPULATION_SIZE);
DOTS{n,ih} = HULL_VECTORS(:,:,n,ih) * HULL_VECTORS(:,:,n,ih)';

end
end