how to filter rows with zero values?

10 ビュー (過去 30 日間)
Mounira
Mounira 2024 年 1 月 25 日
コメント済み: Rishi 2024 年 1 月 25 日
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 Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by