how to filter rows with zero values?

12 次查看(过去 30 天)
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

采纳的回答

Rishi
Rishi 2024-1-25
编辑:Rishi 2024-1-25
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 个评论
Rishi
Rishi 2024-1-25
Edited the code. 'rowsWithIrradiation' was just the negation of 'rowsWithZeroIrradiation'.
If this solved your query, please accept the answer.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by