How can I delete row based on the value of row (i) and row (i+1)

1 回表示 (過去 30 日間)
Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed 2018 年 10 月 25 日
Hi I am new to matlab, I am working with 1000*10 matrix. I want to delete the all row if the value in row (i) column 10 is the same as the value in row (i+1) column 10, and keep the row if the value in row (i) column 10 is not equal to the value in row (i+1) column 10. For example: my data file is X. and my code is, but it does not work with me :
for i=1:length (X)
if (X(i,10)~=X(i+1,10)
y(j,1:10)= X(i,1:10);
j=j+1;
else
y(j,1:10)=[];
end
if true
% code
end
end

採用された回答

James Tursa
James Tursa 2018 年 10 月 25 日
編集済み: James Tursa 2018 年 10 月 25 日
I think this is what you are asking for (i.e., delete all rows that have equal neighbors in column 10):
X = your matrix
n = 10; % the column to use for the comparison
d = diff(X(:,n)); % the 0's in d indicate where neighbors are equal to each other
d = [d;1] .* [1;d]; % vector that has 0's for all rows that are equal to neighbors
X(~d,:) = []; % delete the rows that have equal neighbors using logical indexing

その他の回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 25 日
for i=1:length (X)
if (X(i,10)~=X(i+1,10)
y(i,:)= X(i,:);
else
y(i,:)=[];
end
end
  2 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 25 日
編集済み: madhan ravi 2018 年 10 月 26 日
Note: I didn’t per-allocate the array because the resultant matrix is unknown since you didn’t provide data but if you know the resultant matrix you can simply do
y =zeros(1000,10)
Gadelhag M Omar Mohmed
Gadelhag M Omar Mohmed 2018 年 10 月 29 日
thanks for your help

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by