Deleting rows in a matrix

1 Ansicht (letzte 30 Tage)
Bas
Bas am 11 Aug. 2015
Kommentiert: Brendan Hamm am 11 Aug. 2015
Hi,
I have two matrices which are generated in a loop for n years:
A (m-rows x n-columns) B (k-rows)
Matrix B includes values which represent row numbers of matrix A that should be deleted. So, I want for every value in matrix B, the corresponding row in matrix A to be deleted.
For example,
A=[1 2; 2 2; 3 4], B= [2]
The result should be a matrix [1 2;3 4]. I.e. the middle row is deleted as B includes value 2.
For some reason I'm not able to find the correct coding for this. I tried several things, but inside the loop it doesn't work. This is what I tried, but this does't work:
for j= 1:n
B;
for i= 1:length(B)
indices = find(A(:,1)==B(i));
A(indices,:) = [];
end
end
Can anyone help me with this?
  2 Kommentare
James Tursa
James Tursa am 11 Aug. 2015
Does B really contain the row numbers, or does it contain values that must match values in the first column of A? Your words say the former but the code looks like the latter.
Bas
Bas am 11 Aug. 2015
Sorry, B includes 0s and values that should match values in the first column of A..

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Brendan Hamm
Brendan Hamm am 11 Aug. 2015
If you want to delete the second row because B has the value 2 you can use this vector to index into A as such:
A(B,:) = [];
That is how I interpret the question you pose, but the code seems to approach a different problem, outlined below.
If you want to delete the second row because the first element of A is 2, then you would find if each element of the first column of A is in the vector B and delete those locations:
locA = ismember(A(:,1),B); % Logical vector (true if A(i,1) is in B, false otherwise)
% use the logical vector to index into A and delete
A(locA,:) = [];
  2 Kommentare
Bas
Bas am 11 Aug. 2015
Bearbeitet: Bas am 11 Aug. 2015
Thanks for the reply. It's not working yet. As you can see below, matrix UitDienst contains zero values and some non zero values. These non zero values correspond to crew IDs which are defined in a(:,1). I want the rows in matrix a(i,:) corresponding to the non zero values from matrix UitDienst for each forecasting year j to be deleted. So, I want to have matrix a to be saved in excel after each forecasting year in the loop.
Unfortunately, script below is not working. Can you help?
Brendan Hamm
Brendan Hamm am 11 Aug. 2015
I am a bit confused with your question here still, what is not working specifically? Does this work fine on the first iteration of the loop? Is it that you are overwriting the excel file at every iteration of the loop? Are you receiving any errors?
Taking a stab at it:
If UitDienst is a matrix and not a vector as in the example you gave, then you likely do not want to perform a linear indexing as you do with
UitDienst(j)
Matlab is a column major language which means that elements of a matrix are stored column by column:
A = [1 2 3; 4 5 6]
is stored in memory as [1 4 2 5 3 6] (column-by-column) This means that A(2) will return the value 4, A(3) will return the value 2, etc. It seems that maybe you need to change that to read:
UitDienst(:,j)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

James Tursa
James Tursa am 11 Aug. 2015
Bearbeitet: James Tursa am 11 Aug. 2015
"Matrix B includes values which represent row numbers of matrix A that should be deleted"
Assuming this is exactly what you want, then this will do the job:
A(B,:) = [];

Jon
Jon am 11 Aug. 2015
Bearbeitet: Jon am 11 Aug. 2015
Your code produces the result you expect. You'll need to better specify your problem. You can lose the loop completely, though, with a simple indexing:
A(B,:) = [];
This is assuming that B is unique and contains no elements larger than the length of A.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by