Finding minimum output value for same inputs in a table

1 view (last 30 days)
antje668
antje668 on 28 Mar 2022
Commented: antje668 on 28 Mar 2022
I am a new user of matlab and have a question about tables.
So I have a table called myTable with three different inputs (x,y,z) and one output (fct_total), for some of the inputs I sometimes have different outputs (see higlighted rows in screenshot)
I wonder if there is any way using for example logical indexing to select the smallest output (fct_total) in the cases where inputs are the same and then place the inputs and the minimum value of the output in a separate table?

Accepted Answer

Scott MacKenzie
Scott MacKenzie on 28 Mar 2022
Is your table consistently organized as in the image; i.e., 36 rows repeating with the same values execpt for the last column (fct_total)? If so, this code will create a column vector containing the miminum values in the fct_total column for each x-y-z combination:
d = reshape(myTable.fct_total, 36, []);
min_d = min(d, [], 2);
Then, create a new table using the min_d values and the x-y-z combinations.
  1 Comment
antje668
antje668 on 28 Mar 2022
Hi Scott,
Thank you for your reply and I realized that I should have been more specific, there are also other outputs, but these I want to keep exactly as they are (See screenshot 1).
So: I want to find the minimum value for fct_total at the points where x, y and z have the same values and at the same time keep the other outputs as they are at the point where fct_total is minimized.
And it's not always consistent as in the figure in my previous post. After one iteration, a table can look like screenshot 2 (6 rows in total), where I have highlighted the rows with the same x y z values.

Sign in to comment.

More Answers (1)

Davide Masiello
Davide Masiello on 28 Mar 2022
Edited: Davide Masiello on 28 Mar 2022
clear,clc
% The following matrix has two rows which have same x-y-z but different
% output (namely, first and last rows)
A = rand(9,4);
A = [A;A(1,:)];
A(end,end) = A(1,end)*2
A = 10×4
0.9887 0.6494 0.1059 0.8865 0.0213 0.2678 0.9163 0.4090 0.4932 0.3351 0.9426 0.5517 0.7167 0.2276 0.1898 0.4658 0.3403 0.8453 0.0329 0.9692 0.2633 0.0325 0.0697 0.7364 0.4317 0.2285 0.7808 0.2586 0.4510 0.7312 0.0857 0.6937 0.1473 0.2083 0.2359 0.9424 0.9887 0.6494 0.1059 1.7730
% Extract the smallest of the two values
xyz = A(1,1:3); % This is the input that has two possible outputs
output = min(A(all(A(:,1:3)==xyz,2))) % Mimimum output
output = 0.9887

Community Treasure Hunt

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

Start Hunting!