Vector Manipulation

조회 수: 10 (최근 30일)
DJ
DJ 2011년 7월 22일
Hi Everyone,
I have a vector that is an index containing row numbers that need to be removed from a dataset. However, due the nature of my data, I know that not only do these rows need to be removed, but also the 30 rows after any of them.
My question is:
How would I add new entries to the existing vector to include each of the 30 rows following any existing entry. There should end up being 30 new entries for every existing one and I could then unique(vector) to remove any duplicate row numbers.
I hope this isn't too much of a doit4me. I don't really use anything other than dataset arrays from the statistics toolbox, so my vector manipulation skills are sort of limited.

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 7월 22일
row_index = [1;33;67]; %row indices
row_index = unique(cumsum(horzcat(row_index,ones(size(row_index,1),30)),2))
I threw the unique in there in case you have say (15,30) where 15 of the 15's values would overlap with some of the 30's. If there's no fear of that:
row_index = reshape(cumsum(horzcat(row_index,ones(size(row_index,1),30)),2).',[],1)
Definitely not a doit4me, as you posed your problem well, asked politely, made it interesting (matrix manipulation usually is).
  댓글 수: 5
DJ
DJ 2011년 7월 26일
I just ran into the index exceeding the size of data problem actually. The the last row contained a bad entry. If any of the last 29 rows were in the index, it would exceed the size of the data :s.
Sean de Wolski
Sean de Wolski 2011년 8월 1일
I know this is old but I've been away from the computer for a very happy 10 days.
Use min() with the row size so if it's exceeded it's deleted.
row_index = min(row_index,size(A,1));

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

추가 답변 (1개)

Fangjun Jiang
Fangjun Jiang 2011년 7월 22일
The best way is to provide some example data so we can understand your question better. Below is my guess.
Data=(1:1000)';
Index=1:100:1000
for k=1:length(Index)
EndIndex=min(Index(k)+30,length(Data));
% remove
%Data(Index(k):EndIndex)=[];
% or provide new data
Data(Index(k):EndIndex)=0;
end
  댓글 수: 2
Sean de Wolski
Sean de Wolski 2011년 7월 22일
Removing won't work as the vector will change positions on each deletion. You'd either have to run it backwards (where there would still be inconsistencies with duplicates) or build an index vector and to the deletion all at once. The deletion all at once is the best method since you won't be resizing the array on each iteration.
Fangjun Jiang
Fangjun Jiang 2011년 7월 22일
Good point, thank you! I missed it.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by