How to remove rows in a multidimensional matrix?
Mostra commenti meno recenti
I have a three dimensional matrix (A) and I want to remove rows that contain 0. I tried to do this in a 'for loop', but it gives an error "A null assignment can have only one non-colon index.". When I replace [] with NaN, it seems the code and index are right. Does it possible to do this outside of 'for loop'? Thank you in advance for your help.
clear all
clc
A1 = [50, 1, 130; 50, 1, 140; 0, 0, 0; 0, 0, 0];
A2 = [0, 0, 0; 51, 2, 131; 51, 2, 141; 0, 0, 0];
A3 = [0, 0, 0; 0, 0, 0; 52, 3, 132; 52, 3, 142];
A (:,:,1)= A1;
A (:,:,2) = A2;
A (:,:,3) = A3;
disp (A)
[r,c] = size(A);
for i = 1:3
for h = 1:r
if A (h,:,i) == 0
A (h,:,i) = [] ; % = NaN
end
end
end
disp(A)
Risposta accettata
Più risposte (1)
Image Analyst
il 19 Lug 2015
A "row" is really a plane. For example row 2 will occur for every value of column and slice, so it's really a plane. That is A(2, :, :) is a plane along the x-z plane. To get rid of "row" 2, use []:
A(2, :, :) = [];
Any element which would have appeared with 2 as the first index (the "row" index) will now be gone.
5 Commenti
Ali Y.
il 19 Lug 2015
John D'Errico
il 19 Lug 2015
Deletion DOES work. I think the problem is that when you delete one "row" of a multidimensional matrix, you are also deleting that same row from other parts of the matrix, since a row is really a plane. matrices MUST be rectangular. you cannot delete one row from one plane, but not the corresponding row from another plane.
A = rand(4,3,2)
A(:,:,1) =
0.81472 0.63236 0.95751
0.90579 0.09754 0.96489
0.12699 0.2785 0.15761
0.91338 0.54688 0.97059
A(:,:,2) =
0.95717 0.42176 0.65574
0.48538 0.91574 0.035712
0.80028 0.79221 0.84913
0.14189 0.95949 0.93399
A(1,:,:) = []
A(:,:,1) =
0.90579 0.09754 0.96489
0.12699 0.2785 0.15761
0.91338 0.54688 0.97059
A(:,:,2) =
0.48538 0.91574 0.035712
0.80028 0.79221 0.84913
0.14189 0.95949 0.93399
So deletion worked just fine. I deleted the first row of each plane. I cannot delete the first row of A(:,:,1) but NOT from A(:,:,2).
Image Analyst
il 19 Lug 2015
I'm not sure what you want. I did delete a "row". What started as a 4 row by 3 column by 3 slice array turned into a 3 row by 3 column by 3 slice array. There is now one row fewer than before so it did indeed get deleted. Why do you say the deletion does not work? By the way, it wasn't just a suggestion, I actually did try it before I posted, to prove it.
Ali Y.
il 19 Lug 2015
John D'Errico
il 19 Lug 2015
And my comment is you CANNOT delete one row from the first plane, and NOT from the second plane!!!!!!! Trying to do so would have the effect of creating a non-rectangular matrix.
If you absolutely need to do this, then you need to consider a different structure for your data storage. Perhaps use a cell array of rectangular matrices, which can then be different sizes. Any sizes at all in fact.
Categorie
Scopri di più su Matrix Indexing in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!