Selecting rows based on two criteria

38 views (last 30 days)
I need to work out how effective a pump is at removing 4 contaminants (Values A:D).
However, I have two pumps and need to work out how effective they are at different speeds and in isolation. I have four settings:
  • 0 = pump off (at switch)
  • 0.5 = pump off (below necessary conc for activation)
  • 0.75 = pump on (half speed)
  • 1 = pump on (full speed)
I need to know how each of the two pumps operate at removing the 4 contaminants for the following scenarios:
  • Pump is 0.75 AND other pump is 0.5
  • Pump is 1 AND other pump is 0
  • Pump is 1 OR 0.75 AND other pump is 0.5 OR 0
There are a few more possibilites, but there is not need to list them all.
How do I select an entire row of variables is multiple AND, OR demands are met in this way?
I have included some example data below and attached a .mat file but the actual data set splits time up per second and is far larger.
Value A Value B Value C Value D Pump 1 Pump 2
16/10/2019 12:00 1 4 5 6 1 0.5
16/10/2019 18:00 4 5 6 14 0.75 0.5
17/10/2019 00:00 1 4 5 3 1 0.5
17/10/2019 06:00 4 2 3 5 0.75 0
17/10/2019 12:00 1 5 2 6 0 0.75
17/10/2019 18:00 4 3 4 6 0.5 1
18/10/2019 00:00 3 2 3 3 0.5 1
18/10/2019 06:00 4 4 2 6 0.5 1
  2 Comments
William Garrett
William Garrett on 8 Jan 2020
Thanks, I have attached a .mat file

Sign in to comment.

Accepted Answer

Meg Noah
Meg Noah on 8 Jan 2020
Hi, here's a solution that makes this assumption - your columns Pump On and Pump Off are really Pump1 and Pump2. Otherwise, I couldn't figure out what values you had for the two different pumps. I thought that on and off are the values in those columns.
clc
close all
clear all
VarNames = {'Datenum','ValueA','ValueB','ValueC','ValueD','Pump1','Pump2'};
values = { ...
datenum(2019,10,16,12,00,00), 1, 4, 5, 6, 1, 0.5; ...
datenum(2019,10,16,18,00,00), 4, 5, 6, 14, 0.75, 0.5; ...
datenum(2019,10,17,00,00,00), 1, 4, 5, 3, 1, 0.5; ...
datenum(2019,10,17,06,00,00), 4, 2, 3, 5, 0.75, 0; ...
datenum(2019,10,17,12,00,00), 1, 5, 2, 6, 0, 0.75; ...
datenum(2019,10,17,18,00,00), 4, 3, 4, 6, 0.5, 1; ...
datenum(2019,10,18,00,00,00), 3, 2, 3, 3, 0.5, 1; ...
datenum(2019,10,18,06,00,00), 4, 4, 2, 6, 0.5, 1};
pumpTable = cell2table(values,'VariableNames',VarNames);
% 0) summarize table
summary(pumpTable)
% 1) one way to query and display
fprintf(1,'Pump1 is 0.75 AND Pump2 is 0.5\n');
idx = find(pumpTable.Pump1 == 0.75 & pumpTable.Pump2 == 0.5);
if (~isempty(idx))
for ival = 1:numel(idx)
fprintf(1,'%s Contaminants A = %.1f B = %.1f C = %.1f D = %.1f\n', ...
datestr(pumpTable.Datenum(idx(ival)),'dd/mm/yyyy HH:MM'), ...
pumpTable.ValueA(idx(ival)),pumpTable.ValueB(idx(ival)), ...
pumpTable.ValueC(idx(ival)),pumpTable.ValueD(idx(ival)));
end
else
disp('No records meet the search criteria!');
end
% 2) another way to query and display
fprintf(1,'\nPump1 is 1 AND Pump2 is 0\n');
idx = find(pumpTable.Pump1 == 1 & pumpTable.Pump2 == 0);
if (~isempty(idx))
for ival = 1:numel(idx)
fprintf(1,'%s Contaminants A = %.1f B = %.1f C = %.1f D = %.1f\n', ...
datestr(pumpTable.Datenum(idx(ival)),'dd/mm/yyyy HH:MM'), ...
pumpTable.ValueA(idx(ival)),pumpTable.ValueB(idx(ival)), ...
pumpTable.ValueC(idx(ival)),pumpTable.ValueD(idx(ival)));
end
else
disp('No records meet the search criteria!');
end
% 3) another way to query and display
fprintf(1,'\nPump1 is 0.75 AND Pump2 is 0 or 0.5\n');
idx = find(pumpTable.Pump1 == 1 & (pumpTable.Pump2 == 0 | pumpTable.Pump2 == 0.5));
if (~isempty(idx))
% summary(pumpTable(idx,:))
for ival = 1:numel(idx)
fprintf(1,'%s Contaminants A = %.1f B = %.1f C = %.1f D = %.1f\n', ...
datestr(pumpTable.Datenum(idx(ival)),'dd/mm/yyyy HH:MM'), ...
pumpTable.ValueA(idx(ival)),pumpTable.ValueB(idx(ival)), ...
pumpTable.ValueC(idx(ival)),pumpTable.ValueD(idx(ival)));
end
else
disp('No records meet the search criteria!');
end
  3 Comments
William Garrett
William Garrett on 9 Jan 2020
I think this is exactly what I needed. I have marked the answer as correct. Thank you ever so much for your help

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!