how to filter rows with zero values?

12 views (last 30 days)
Mounira
Mounira on 25 Jan 2024
Commented: Rishi on 25 Jan 2024
hello,
so i have data in mat.file 3 columns about weather conditions (first column: temperature, second column: irradiation1; third column: irradiation2 and fourth column= pv power output); i also have half the data with zero irradiation and zero output; but the temperature is not zero, so my question is how to deleted all the rows with zero irradiation; and if is there a way to only delete 90 % of these row, and keep like 10 %?
x=
0.0015 0.0044 0.0041 0.0102
0.0013 0 0 0
0.0011 0 0 0
0.0011 0 0 0
0.0010 0 0 0
0.0009 0 0 0
0.0008 0 0 0
0.0007 0 0 0
0.0006 0 0 0
0.0006 0 0 0
0.0006 0 0 0
0.0006 0 0 0
0.0006 0 0 0
0.0007 0.0005 0.0005 0.0002
0.0007 0.0061 0.0054 0.0175

Accepted Answer

Rishi
Rishi on 25 Jan 2024
Edited: Rishi on 25 Jan 2024
Hi Kawsar,
I understand you want to edit your file such that 90% of rows with 0 irradiation are removed.
You can use logical indexing to select the rows which have 0 irradiation. After that, you can use the 'randsample' function to select 10% of such rows. Here is the code to do so:
% Find rows where both irradiation columns are zero
rowsWithZeroIrradiation = all(x(:, 2:3) == 0, 2);
numZeroRows = sum(rowsWithZeroIrradiation);
numRowsToKeep = round(0.1 * numZeroRows);
zeroIrradiationIndices = find(rowsWithZeroIrradiation);
% Randomly select 10% of these indices to keep
indicesToKeep = randsample(zeroIrradiationIndices, numRowsToKeep);
logicalIndexToKeep = false(size(x, 1), 1);
logicalIndexToKeep(indicesToKeep) = true;
finalRowsToKeep = logicalIndexToKeep | ~rowsWithZeroIrradiation;
x = x(finalRowsToKeep, :);
You can learn more about logical indexing from the below documentation:
Hope this helps!
  1 Comment
Rishi
Rishi on 25 Jan 2024
Edited the code. 'rowsWithIrradiation' was just the negation of 'rowsWithZeroIrradiation'.
If this solved your query, please accept the answer.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!