Clear Filters
Clear Filters

Compair two vectors with different length and different values??

6 views (last 30 days)
Hallo
Lets say i have two vectors A, B with different length (Length(A) not equal to Length(B)) and the Values in Vector A, are not the same as in Vector B. i want to compair each value of B with Values of A (Compair means if Value B(i) is alomst the same value of A(1:end) for example B(i)-Tolerance<A(i)<B(i)+Tolerance.
How Can i do this without using For loop as the data are huge?
I know ismember, intersect,repmat,find but non of those function can really help me
  2 Comments
Jos (10584)
Jos (10584) on 12 Jul 2013
Huge is relative ... How would you do this using a for-loop? It might be easy to improve upon that.
What is it exactly you want to know. Your wordings suggest that you only want to see if an element of B is close one (or more) elements of A, while your formula suggests that you want it the other way around ...
Aubai
Aubai on 12 Jul 2013
Edited: Aubai on 12 Jul 2013
So i have a wt Values (on Y-axie) of one signal with Sampling frequency of F1, B Matris is 594301*1 double and the second matrix is also a wt Values (on Y-axie) with Sampling frequency of F2, A Matris is 713164*1 double. there is a time shift between the two wt function that i would like to calculate. for that i am trying to find the equality values of B Matrix with A Matrix (Like resample) and then getting the index of those A values equale to the B values in order to get the X- axie corresponding values (time) and finaly calcualte the time difference. with for i did it as the following:
A = Angle_Measured_OMERCON;
B = TTy;
RESULTS = [];
for i = 1: length(A)
for j = 1:length(B)
if A(i) == B(j)
Results(i,j) = i;
elseif B(j)-1 < A(i) < B(j)+1
Results(i,j) = i;
end
end
end
end

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Jul 2013
This is a job for Bruno's FEX: ismemberf.
  5 Comments
Jan
Jan on 13 Jul 2013
The help section of ismemberf is actually clear.
A = 0:0.1:1
B = 0.32
[tf, loc] = ismemberf(B, A, 'tol', 0.02) % "tol", not "tot"!
Now tf is true, because B is found inside a, and loc is 4, because it is found at the 4th element of A.
Aubai
Aubai on 16 Jul 2013
Thx for the support: It seems this is how i can do it
if true
A_diff_2 = diff(A,2);
B_diff_2 = diff(B,2);
A_diff_max = max(A_diff_2);
B_diff_max = max(B_diff_2);
Zero_Crossing_Points_A = find(A_diff_2 >= 178);
First_Point_of_calculation_A = Zero_Crossing_Points_A(1);
Zero_Crossing_Points_B = find(B_diff_2 >= 359);
First_Point_of_calculation_B = Zero_Crossing_Points_B(2);
i = 1;
Delta_final_time = [];
Delta_final_Angle = [];
time_Final = [];
%------------------------ Try ------------------------------
while i<=length(Zero_Crossing_Points_B)
if i == 1395
haha = 2;
end
if i == 1
A_cut = A(i:Zero_Crossing_Points_A(i));
A_time_cut = A_time(i:Zero_Crossing_Points_A(i));
B_cut = B(i:Zero_Crossing_Points_B(i));
B_time_cut = B_time(i:Zero_Crossing_Points_B(i));
[tf, loc] = ismemberf(B_cut, A_cut, 'tol', 1);
if tf ~= 1
error('Worrning','One of the CAN values was not recognized by OMERCON values')
else
Delta = B_time_cut - A_time_cut(loc);
Delta_time = mean([A_time_cut(loc) B_time_cut]');
Delta_final_time = [Delta_final_time,Delta];
Delta_final_Angle = [((Delta*360)/0.02) ,Delta_final_Angle];
time_Final = [time_Final,Delta_time'];
%tryy(i,1:length(Delta_final_Angle)) = Delta_final_Angle(1:end);
end
else
A_cut = A(Zero_Crossing_Points_A(i-1):Zero_Crossing_Points_A(i));
A_time_cut = A_time(Zero_Crossing_Points_A(i-1):Zero_Crossing_Points_A(i));
B_cut = B(Zero_Crossing_Points_B(i-1):Zero_Crossing_Points_B(i));
B_time_cut = B_time(Zero_Crossing_Points_B(i-1):Zero_Crossing_Points_B(i));
if length(A_cut) > 200
A_cut = A_cut(2:end);
A_time_cut = A_time_cut(2:end);
end
if length(B_cut) > 17
B_cut = B_cut(2:end);
B_time_cut = B_time_cut(2:end);
end
[tf, loc] = ismemberf(B_cut, A_cut, 'tol', 1.6);
if tf ~= 1
error('Worrning','One of the CAN values was not recognized by OMERCON values')
else
Delta = B_time_cut - A_time_cut(loc);
Delta_Angle = (Delta*360)/0.02;
Delta_time = mean([A_time_cut(loc) B_time_cut]');
if length(Delta) < 17
Delta(end+1:17) = 0;
Delta_time(end+1:17) = 0;
Delta_Angle(end+1:17) = 0;
%Delta = [Delta(1:end),zeros(length(Delta)+1:17)];
Delta_final_time = [Delta_final_time,Delta];
Delta_final_Angle = [Delta_final_Angle,((Delta*360)/0.02)];
time_Final = [time_Final,Delta_time'];
%tryy(i,1:length(Delta_Angle)) = Delta_Angle(1:end);
else
Delta_final_time = [Delta_final_time,Delta];
Delta_final_Angle = [Delta_final_Angle,((Delta*360)/0.02)];
time_Final = [time_Final,Delta_time'];
%tryy(i,1:length(Delta_Angle)) = Delta_Angle(1:end);
end
end
end
i = i + 1;
end
Delta_N_Loc = find(Delta_final_Angle < 0);
Delta_N_Loc_New = Delta_N_Loc + 1;
Delta_final_Angle(Delta_final_Angle < 0) = Delta_final_Angle(Delta_N_Loc_New);
time_Zeros = find(time_Final == 0);
time_Zeros_P_1 = time_Zeros + 1;
time_Zeros_N_1 = time_Zeros - 1;
time_Final(time_Final == 0) = ((time_Final(time_Zeros_N_1))+(time_Final(time_Zeros_P_1)))/2;
Delta_final_Angle_Zeros = find(Delta_final_Angle == 0);
Delta_final_Angle_Zeros_P_1 = Delta_final_Angle_Zeros + 1;
Delta_final_Angle_Zeros_N_1 = Delta_final_Angle_Zeros - 1;
Delta_final_Angle(Delta_final_Angle == 0) = ((Delta_final_Angle(Delta_final_Angle_Zeros_N_1))+(Delta_final_Angle(Delta_final_Angle_Zeros_P_1)))/2;
Delta_final_Angle_Round = round(Delta_final_Angle);
time_Final_r = reshape(time_Final,numel(time_Final),1);
Delta_final_Angle_r = reshape(Delta_final_Angle_Round,numel(Delta_final_Angle_Round),1);
if length(Delta_final_Angle_r) > length(time_Final_r)
Delta_final_Angle_r = Delta_final_Angle_r(1:length(time_Final_r));
end
end

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 12 Jul 2013
A = randi(25,10,1);
B = randi(30,20,1);
tolerance = 1;
out = abs(bsxfun(@minus,B(:),A(:).')) < tolerance;
  2 Comments
Aubai
Aubai on 12 Jul 2013
Edited: Aubai on 12 Jul 2013
Thanks for the fast replay i am now getting an out of memory error when perofrming this type of operation!! how can i over come that. note: A (713164*1 double) B (594301*1 double)

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!