MATLAB Answers

Compare values in two matrix with different length by time in first row

1 view (last 30 days)
Nik Rocky
Nik Rocky on 11 Feb 2021
Commented: Nik Rocky on 17 Feb 2021
Hello,
I have one aim to do comparison of values in two matrices without using for loops (array are huge).
I show you example with two small arrays:
Matrix_R =
9 x 2 double
[time value]
0.020 77
0.030 98
0.040 79
0.070 95
0.080 96
0.090 99
0.100 100
0.100 200
0.120 44
Matrix_G =
13 x 2 double
[time value]
0.010 75
0.020 75
0.030 75
0.040 77
0.050 78
0.060 96
0.070 99
0.080 100
0.090 100
0.100 110
0.110 100
0.120 100
0.130 110
Matrix_R (measure) has not homogenous time values
Matrix_G (ground truth) has homogenous time values
I want compare values between two matrices (with tolerance) from second column by same time. Comparison should be work in both directions.
Example (value tolerance 10):
at time 0.010 in Matrix_G is value 75, in Matrix_R there are no value -> False negative
at time 0.020 in Matrix_G is value 75, in Matrix_R is value 77 -> True positive
at time 0.030 in Matrix_G is value 75, in Matrix_R is value 98 -> False negative and False positive
Question:
  • Can I use ismembertool by matrix with different lengths?
  • Maybe it makes sense to change time rows of Matrix_R to homogenous and take 0 like values?
Matrix_R_mod =
9 x 2 double
[time value]
0.010 0
0.020 77
0.030 98
0.040 79
0.050 0
0.060 0
0.070 95
0.080 96
0.090 99
0.100 100
0.100 200
0.110 0
0.120 44
what can I make with double answers of time?
I will be happy to hear some ideas from you! Thank you!

Accepted Answer

Abhishek Gupta
Abhishek Gupta on 15 Feb 2021
Hi,
You can perform the task mentioned above as follows: -
clc; clear;
%% Input variables
Matrix_R = [0.020 77; 0.030 98; 0.040 79; 0.070 95; 0.080 96; ...
0.090 99; 0.100 100; 0.100 200; 0.120 44];
Matrix_G = [0.010 75; 0.020 75; 0.030 75; 0.040 77; 0.050 78; 0.060 96; ...
0.070 99; 0.080 100; 0.090 100; 0.100 110; 0.110 100; 0.120 100; 0.130 110];
tol = 10;
%% Initialize output matrix, where
% column 1 -- all the elements in the Matrix_G with appropriate repetition
% colum 2 -- output column, where -1 -> false negative; 1 -> true positive; 0 -> False negative and False positive
freq = [Matrix_G(:,1), histc(Matrix_R(:,1),Matrix_G(:,1))]; % get frequncy of each element of Matrix_G in Matrix_R
repetitionNumber = freq(freq(:,2) > 1, 1); % get the element which repeats more than once
repetitionTimes = freq(freq(:,2) > 1, 2); % get number of time element repeats more than once
outputTime = sort([freq(:,1); repelem(repetitionNumber, repetitionTimes - 1)]); % sort w.r.t time
output = [outputTime -1*ones(size(outputTime,1), 1)]; % initialize output matrix with all time rows == -1
%%
% get common elements between Matrix_R and Matrix_G
[Lia,Locb] = ismember(Matrix_R(:,1), Matrix_G(:,1));
A = Matrix_R(Lia, 2);
B = Matrix_G(Locb, 2);
toleranceCheck = abs(A-B) <= 10; % find elements which are within the tolerance
output(ismember(output(:,1), Matrix_R(:,1)), 2) = toleranceCheck;

More Answers (0)

Community Treasure Hunt

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

Start Hunting!