how to delete the first n data entries from each column of a vector?

18 Ansichten (letzte 30 Tage)
Daniel Hooker
Daniel Hooker am 10 Mär. 2017
Kommentiert: Star Strider am 11 Mär. 2017
I have a large vector of data (1600 x 240), and I was wondering if anyone knows how to delete the first n number of entries from each column of this vector.
As the data entries do not start at the same date for each column I cannot simply delete the first n rows, as for some columns the data has not started by then.
thanks in advance

Antworten (2)

Star Strider
Star Strider am 10 Mär. 2017
I’m not certain exactly what you want.
See if this works:
Matrix = rand(5); % Create Data
n = 2; % Assign ‘n’
Matrix(1:n,:) = []; % Result (Delete First ‘n’ Rows From All Columns)
  2 Kommentare
Daniel Hooker
Daniel Hooker am 11 Mär. 2017
Hi thanks for your help.
The problem is that what you suggests deletes the first n rows from the whole dataset, however what I am trying to achieve is to delete the first 36 data entries from each column. The tricky part is that the data entries do not start in the same row for each column (i have attached a screenshot of the excel document I am importing if that makes it any clearer)
thanks again
Star Strider
Star Strider am 11 Mär. 2017
When xlsread imports the file, the empty elements will be read as NaN. The constraint here is that the size of the matrix must not change, so the only way to do what you want is to set the rows in each column that you want to remove to NaN as well. You can then deal with the NaN values (that you will have to deal with regardless) any way you want.
I can’t find a way to do this without a loop. This is the most efficient way I can come up with to do what you want.
The Code
A = [NaN NaN NaN 1 NaN 2 3; NaN NaN NaN 1 NaN 2 3; NaN 4 NaN 1 NaN 2 3; NaN 20 NaN 1 17 2 3; 6 7 8 9 10 11 12; randi(50, 4, 7)];
Anan = ~isnan(A); % Logical Vector
[Ananr,Ananc] = find(Anan); % Indices Of ‘~isnan’
StartRow = accumarray(Ananc, Ananr, [], @min); % First ‘non-NaN’ Row
RemoveRows = 2; % Rows To Remove
Anew = A; % Retain Reference
for k1 = 1:length(StartRow)
IdxRng = StartRow(k1):StartRow(k1)+RemoveRows-1;
Anew(IdxRng,k1) = NaN; % Set Removed Rows To ‘NaN’
end
produces:
A =
NaN NaN NaN 1 NaN 2 3
NaN NaN NaN 1 NaN 2 3
NaN 4 NaN 1 NaN 2 3
NaN 20 NaN 1 17 2 3
6 7 8 9 10 11 12
21 22 6 17 37 48 36
33 1 19 48 14 21 34
32 50 10 47 22 50 27
15 9 25 3 28 16 35
Anew =
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN
NaN NaN NaN 1 NaN 2 3
NaN NaN NaN 1 NaN 2 3
NaN 7 NaN 9 NaN 11 12
NaN 22 NaN 17 37 48 36
33 1 19 48 14 21 34
32 50 10 47 22 50 27
15 9 25 3 28 16 35
The code first produces a separate logical index array (not necessary, it can be combined in the find call, I did it for diagnostic purposes), then find the minimum start indices of the rows for each column with the accumarray call. It then loops through those and replaces the ‘RemmoveRows’ number of rows in each row with NaN values, effectively eliminating them.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 11 Mär. 2017
Try this:
Ri_Rf = Ri_Rf(n+1:end, :); % Extract from rows (n+1) until the bottom.
  1 Kommentar
Daniel Hooker
Daniel Hooker am 11 Mär. 2017
Hi thanks for your help.
The problem is that what you suggests deletes the first n rows from the whole dataset, however what I am trying to achieve is to delete the first 36 data entries from each column. The tricky part is that the data entries do not start in the same row for each column (i have attached a screenshot of the excel document I am importing if that makes it any clearer)
thanks again

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by