Filter löschen
Filter löschen

How to filter by row and print out different row if within boundary.

2 Ansichten (letzte 30 Tage)
Santiago Poretti
Santiago Poretti am 3 Jun. 2021
Beantwortet: vidyesh am 22 Feb. 2024
OutputData = xlsread('dataOutput.csv');
TrialOrder = xlsread('TrialOrder.csv');
% Run Data %
minVal = 33;
maxVal = 41;
a = size(OutputData,2); % # columns in dataOuput
b = (a + 1)/5; % # of Runs
c = 1; % Counter
for trial = 1:b %For # Trials
h = 0;
for i = 1:31 %For all rows
Gx = OutputData(i,(trial*5)-3);
if(Gx<minVal)
h = h + 1;
end
if(Gx>maxVal)
h = h + 1;
end
end
if (h == 0)
goodFire(1,c) = trial;
c = c+1;
end
trial = trial +1;
end
I have the above code in MATLAB currently. For my project, I am trying to filter out variables outside of my 33<Gx<41 range for every trial. This data is read from an excel csv that is labeled Output Data and layed out as follows:
A B C D E F G H I
1 # Gx Gy Gz 0 # Gx Gy
2 # Gx Gy Gz 0 # Gx Gy
3 # Gx Gy Gz 0 # Gx Gy
4 #
5 #
. #
. #
32 # Gx Gy
Columns F-IO would continue displaying data with the same formatting as columns A-E. This would also expand down to 32 rows not just 1-5.
What I am trying to acomplish is for every trial, the code should ONLY read column 2 from the A-E sqeuence and check to see if the value is within my range. It would then repeat this check for all trials (1-50: repeating the A-E format) and for all rows (1-32). If the trial is within the boundary, it is deemed successful and it should print out its respective 1st column (Column with #). Note: Gx should be within the range for every value in that column. This is why I am checking it column by column for every trial.
To do this, I implimented a counter. First it will determine which trial it is on, then I will set Gx for every row in that trial. Implimenting an if statement, It will check if the value of Gx is outside of the boundary. If it IS outside, increase the counter. If at the end of the trial, the counter reads zero, it should print out the numbers in column A for that trial. Reset the counter, move on to next trial, and repeat.
Currently, it is only printing out the trial number. I have not figured out how to print out the first coulmn of every trial instead. Any suggestions?

Antworten (1)

vidyesh
vidyesh am 22 Feb. 2024
Hi Santiago,
I understand the problem as follows: we have a set of trials, each consisting of 5 columns. The goal is to check if all the values in the second column of each trial are within a specified range. If they are, then the first column of that trial should be added to the 'goodFire' matrix.
To achieve this, we can utilize the following code:
OutputData = readtable('dataOutput.csv');
OutputData = table2array(OutputData(:,2:end));% Remove the first column since it is not part of data
% Run Data %
minVal = 33;
maxVal = 41; % # columns in dataOuput
b = size(OutputData,2)/5; % # of Runs
goodFire = [];
for i = 1:b %For # Trials
% check if values of second row of ith trial are less than maxVAl and
% greater than minVal
if max(OutputData(:,(i-1) * 5 + 2)) < maxVal && min(OutputData(:,(i-1) * 5 + 2)) > minVal
% append first row of ith trial if condtions are satisfied
goodFire = [goodFire, OutputData(:,(i-1) * 5 + 1)];
end
end
disp(goodFire)
40 37 33 39 33 34 34 39 37 37 38 36 39 33 35 35 34 33 37 33 38 35 38 36 35 35 40 38 38 36 33 40
Following changes have been made to the code:
  1. Using the maximum and minimum values of the second column for range comparison, which eliminates the need to compare each individual value.
  2. Removed the 'trial = trial + 1' line because the for-loop inherently handles the incrementation.
  3. The 'goodFire' matrix is now being dynamically appended to, as its size is not known in advance and cannot be predetermined.
Please note that I've used placeholder data to match the provided dimensions for testing purposes. You may need to adjust the data import section of the code to fit the specific format of your dataset.
Hope this helps.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by