How to remove rows in a multidimensional matrix?

6 ビュー (過去 30 日間)
Ali Y.
Ali Y. 2015 年 7 月 19 日
コメント済み: Image Analyst 2015 年 7 月 20 日
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)

採用された回答

Thorsten
Thorsten 2015 年 7 月 19 日
for k=1:size(A,3)
Ak = A(:,:,k);
B(:,:,k)= Ak(sum(Ak')~=0,:);
end
  2 件のコメント
Ali Y.
Ali Y. 2015 年 7 月 20 日
編集済み: Ali Y. 2015 年 7 月 20 日
Thank you for your reply and help. A problem arises when the number of rows in each dimension is different. For example, in the following code TA have various non-zero rows.
For me it is not crucial to store output matrices in one parent one. Actually, by the code I want to divide a main matrix to minor ones. I even tried 'removerows'; but ND arrays, again, is the problem.
clc
clear all
A1 = [1;1;1;2;2;3;3;3;3;4;4;4;5;5];
A2= [1;1;1;1;2;2;2;2;2;2;2;2;3;3];
A3 = (10:10:140); A3 = A3';
A = [A1,A2,A3];
[r,c,n] = size(A);
b = min(A2): max(A2);
for k = 1:r
for i = b
if A2(k) == i;
TA(k,:,i) = A(k,:);
end
end
end
for k1 = 1:size(TA,3)
Ak = TA(:,:,k1);
B(:,:,k1)= Ak(sum(Ak')~=0,:);
end
Image Analyst
Image Analyst 2015 年 7 月 20 日
Then you will have to store your data in a cell array like John said. A cell array must be rectangular like any other array, but the contents of the cells can be of different sizes which, in effect, will allow you to do this complicated thing you want to do. See the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 7 月 19 日
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 件のコメント
Ali Y.
Ali Y. 2015 年 7 月 19 日
In your example deletion works, because in A(1,:,:) you say delete row one in all columns and dimensions, as (1,:,:). My point is to delete separately in different dimensions. I think this is the point of introducing n-dimensional matrix. Besides that as would you noticed I am try that within loop. Please note that, the possibility of deletion deserves a notice since the code justify the EFFECT of multidimensional manipulation of matrix SEPARATELY by NaN.
John D'Errico
John D'Errico 2015 年 7 月 19 日
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.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by