How can I selectively import data from two matrices into a third?

1 Ansicht (letzte 30 Tage)
Alexandra
Alexandra am 7 Jun. 2023
Bearbeitet: rakshit gupta am 9 Jun. 2023
I have two matrices, one of them real data and one of them theoretical data. I want to go through the real data in the first column of the matrix and match it to the theoretical data, also in the first column of the matrix.. When they match, I want to put both columns of the real data and the second and third columns of the theoretical data into a new matrix, called combined_data. An issue between the two matrices is that there can be multiple matches in the theoretical data. If there is multiple matches for a point, I want the match with the lowest sum of the corresponding second and third column entries for that row. I know this probably includes a for loop and some if statements, but I'm not sure where to start. Some sample matrices look like this:
real_data= 126.75 980
240.50 1020
241 300
theoretical_data= 126.8 0 1
126.8 0 2
240.5 1 1
243 1 2
So the combined_data matrix would look like this
126.75 980 0 1
240.50 1020 1 1
241 300 1 1

Antworten (1)

rakshit gupta
rakshit gupta am 7 Jun. 2023
Bearbeitet: rakshit gupta am 9 Jun. 2023
You can refer to the the code below:
% Define the real and theoretical data tables
real_data = [126.75, 980;
240.50, 1020;
241, 300];
theoretical_data = [126.8, 0, 1;
126.8, 0, 2;
240.5, 1, 1;
243, 1, 2];
% Initialize the combined_data matrix and a flag to mark used theoretical data
combined_data = zeros(0, size(real_data, 2) + size(theoretical_data, 2) - 1);
n_theoretical_pts = size(theoretical_data, 1);
theory_used = zeros(n_theoretical_pts, 1);
% Loop through each row of the real data
for i = 1:size(real_data, 1)
% Compute the difference between real and theoretical data's first column
diff = theoretical_data(:, 1) - real_data(i, 1);
% Find the index of the minimum difference
[min_diff, idx] = min(abs(diff));
% Check if the corresponding theoretical data point has already been used
while theory_used(idx) % while true
% Replace the used point's value in the difference vector with Inf to ignore it
diff(idx) = Inf;
% Find the new minimum difference and its index
[min_diff, idx] = min(abs(diff));
end
% Mark the theoretical data point as used
theory_used(idx) = 1;
% Extract the matched data
real_entry = real_data(i, :);
theory_entry = theoretical_data(idx, 2:end);
% Append the matched data to the combined_data matrix
combined_entry = [real_entry, theory_entry];
combined_data = [combined_data; combined_entry];
end
% Display the resulting combined_data matrix
disp(combined_data);
1.0e+03 * 0.1268 0.9800 0 0.0010 0.2405 1.0200 0.0010 0.0010 0.2410 0.3000 0.0010 0.0020
  2 Kommentare
Alexandra
Alexandra am 7 Jun. 2023
Hi!
Thank you for the help. The code you wrote seems to be getting stuck on the line where the diff(idx) is set to infinity to be ignored. Is there another way to set it so that it is ignored without setting it to infinity?
diff(idx)=inf
rakshit gupta
rakshit gupta am 7 Jun. 2023
Another way to ignore it is by setting diff(idx) to NaN instead of infinity. The code seems to be working in R2023a.
%replace inf by NaN
diff(idx) = NaN

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by