Searching Between Vectors to Create a Percent of Equality

3 views (last 30 days)
I need to do the following:
Compare 3 vectors in a certain way and generate a percent of accuracy in which x amount of elements in vector A fits between y amount of elements between vectors B & C. Note that A is has a different and a lesser amount of elements than B & C, while B & C are the same size (and contain nearly twice as many elements). A, B & C are all time values in relative time format (aka time since start).
Here is the current code:
A = Time; %reset the temporary incremented values vector to default
offset = double.empty;
percent = double.empty;
tick = 0;
percentCalc = 0;
Bsize = numel(B);
for b = 0:600
bdisp = 'b = %4.3f \n';
fprintf(bdisp, b);
A = Time-b;
disp(['Increment = ', num2str(A(1))]);
for i = 1:Bsize
if (( A >= B(i) & A <= C(i) )) %I believe right here is the issue
%see if A contains a value that falls between a specific instance of B & C
%if such a value exists, add a tick to be used to calculate a percentage later
tick=tick+1;
else
tick=tick+0;
%otherwise add nothing and continue incrementing through B & C to gather ticks
end
end
percentCalc = tick/Bsize*100;
%calculate the percentage
disp(['ticks = ', num2str(tick)]);
disp(['percentCalc = ', num2str(percentCalc)]);
offset(end+1)= b; %adds an additional element of the offset "a" to the growing vector of "offset" to be used for later comparison
percent(end+1)= percentCalc; %does same thing as previous line.
%reset variables for next go around and verify reset visually
tick = 0;
percentCalc = 0;
disp(['tick reset = ', num2str(tick)]);
disp(['percentCalc reset = ', num2str(percentCalc)]);
disp(['-------------------------------------------']); % spacer
end
Edit: ignore following text in italics: The problem with the current code seems to be that the searching part of the algorithm, aka finding a value of A that falls between a specific instance of B & C, only seems to be finding when values of A are greater than all the values of B & C.
It doesn't seem to be finding any ticks ever, even at a micro level. I believe the culprit is the if statement.
It needs to find if a value of A exists between a given instance of B & C. Everything else in the algorithm seems to be working fine.
I hope that this explanation is clear. Let me know if it is not.
Any ideas on how to conquer/fix this problem? Need a solution as soon as possible. Thx.
Edit: Example (an alignment result should produce at least 1 tick like so; Note: my numbers are much more complicated and have more decimal places+ way less wiggle room between B & C):
A = [2, 17, 35, 50, 75]
B = [1, 5, 15, 25, 30, 34, 45, 47, 61, 69, 74]
C = [3, 6, 16, 27, 32, 36, 47, 49, 63, 70, 75]
Ticks = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] = 3
Percent = 3/11*100 = 27.2727272727
We keep changing the values of vector A by offseting them to evaluate and find the best offset possible since the alignment will never be 100%.
  3 Comments
Matthew
Matthew on 26 Jan 2016
Edited: Matthew on 26 Jan 2016
Midimistro, It doesn't look like your code will do what your example does.
Here's a quick way to do what you suggest in the example. Note that it vectorized your inner for loop and if statement.
A = [2, 17, 35, 50, 75]
B = [1, 5, 15, 25, 30, 34, 45, 47, 61, 69, 74]
C = [3, 6, 16, 27, 32, 36, 47, 49, 63, 70, 75]
ticks = 0;
for a = A
ticks = ticks + sum(a>=B&a<=C);
end
Percent = ticks/numel(B)*100;
Midimistro
Midimistro on 27 Jan 2016
Your approach is much simpler, but it won't work since the matrix dimensions of A vs B & C differ.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 27 Jan 2016
Edited: Guillaume on 27 Jan 2016
This looks related to your previous question (was this ever resolved)?
I've not tried to understand your code, but the result you describe can easily be achieved without a loop:
A = [2, 17, 35, 50, 75]
B = [1, 5, 15, 25, 30, 34, 45, 47, 61, 69, 74]
C = [3, 6, 16, 27, 32, 36, 47, 49, 63, 70, 75]
[AA, BB] = ndgrid(A, B);
[~, CC] = ndgrid(A, C); %or use repmat
ticks = any(AA >= BB & AA <= CC)
percent = sum(ticks)/numel(ticks)
If you want to try different offsets added to A, you can just add the offset directly to AA with no need to recreate the AA, BB, and CC matrices through ndgrid again.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!