How can I delete an entire row/element of a matrix/array and also delete the same row/element from another matrix/array?

조회 수: 2 (최근 30일)
"rawData, "TDT1," and "TDT2" are 1-D arrays/columns of numbers of identical length.
Each array represents a different parameter. The first element of each array represents a measurement taken at a particular time. The second element of each array represents a measurement taken at a new time, and so on.
My problem is that there can be elements in each array equal to "9999," which means that a measurement failed to happen.
  • I need to delete elements where the value is "9999."
  • Then I also need to delete the same elements/rows from the other two arrays/columns.
  • In the end, I need the three arrays to have the same length and no "9999" values.
Does anyone know how I can do this? I have Matlab 2016b. I have so far managed the below, but there are key steps missing.
rawData(rawData == 9999) = nan; TDT1(TDT1 == 9999) = nan; TDT2(TDT2 == 9999) = nan; % Here I turn the number "999" into "nan" in all three arrays.
% I don't know how to delete a "nan" element from one array and also delete the same element from the other two arrays.
Thank you for any help

채택된 답변

Adam Danz
Adam Danz 2020년 8월 10일
편집: Adam Danz 2020년 8월 10일
Since the column vectors should be the same length, I recommend you combine them into a matrix (shown below) or a table.
m = [columnVector1, columnVector2, columnVector3];
Then compute which rows have 9999 values.
removeRowIdx = any(m==9999,2);
Then delete those rows
m(removeRowIdx,:) = [];
or replace with NaN values
m(removeRowIdx,:) = NaN;
If you decide to keep the column vector variables separate, you can use the same approach,
removeRowIdx = columnVector1==9999 | columnVector2==9999 | columnVector3==9999;
columnVector1(removeRowIdx) = []; % or = NaN
columnVector2(removeRowIdx) = []; % or = NaN
columnVector3(removeRowIdx) = []; % or = NaN
  댓글 수: 6
Adam Danz
Adam Danz 2021년 12월 2일
Yes and I'd be happy to help you do the same. Give it a shot and share what you've got if you get stuck.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by