how to filter rows with zero values?

조회 수: 11 (최근 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 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