How to find the index of first and last nonzero elements in each column?

95 ビュー (過去 30 日間)
Rabia Zulfiqar
Rabia Zulfiqar 2020 年 5 月 22 日
コメント済み: Stephen23 2022 年 3 月 25 日
Hi,Is there any way to find the index of first and last nonzero element in a matrix?
I have this matrix
A=[0 0 0 0
0 0 0 0
1 0 4 8
2 0 5 9
3 1 6 7
0 2 7 0
0 0 0 0]
as the first non zero element in 1st column is 3 and and the last is 5. In the second column the the 12th element is the first non zero and the last one is 13th etc
I want to store these values in a matrix in which each row represents the index of first non zero element and the 2nd row shows the index of last non zero elements.
the answer matrix should be like this:
B=[3 12 17 24
5 13 21 26]
How can I do this??
Thankyou for time and consideration.I really appreaciate your help.Kindly guide me

採用された回答

Image Analyst
Image Analyst 2020 年 5 月 22 日
You could probably use
A = [0 0 0 0
0 0 0 0
1 0 4 8
2 0 5 9
3 1 6 7
0 2 7 0
0 0 0 0]
[rows, columns] = size(A)
B = zeros(2, columns)
for col = 1 : size(A, 2)
B(1, col) = find(A(:, col), 1, 'first');
B(2, col) = find(A(:, col), 1, 'last');
end
This gives
B =
3 5 3 3
5 6 6 5
which makes sense to me but I'm puzzled as to how you get
B=[3 12 17 24
5 13 21 26]
for your example. Can you explain?
  4 件のコメント
Rabia Zulfiqar
Rabia Zulfiqar 2020 年 5 月 22 日
編集済み: Rabia Zulfiqar 2020 年 5 月 22 日
Like here I have a matrix of 7x4 but if I have a matrix of 7x4x20 then how can I do this?
Image Analyst
Image Analyst 2020 年 5 月 23 日
Oh, you wanted the "linear index" rather than the row index within each column (as I had assumed). If you'd said "linear index" (the proper MATLAB lingo) I would have gotten it as :
A = [0 0 0 0
0 0 0 0
1 0 4 8
2 0 5 9
3 1 6 7
0 2 7 0
0 0 0 0]
[rows, columns] = size(A)
B = zeros(2, columns)
for col = 1 : size(A, 2)
B(1, col) = find(A(:, col), 1, 'first') + rows * (col - 1);
B(2, col) = find(A(:, col), 1, 'last') + rows * (col - 1);
end
B
but it looks like Stephen figured out, and it even works for higher dimension arrays, so just use his answer.

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

その他の回答 (4 件)

Stephen23
Stephen23 2020 年 5 月 22 日
編集済み: Stephen23 2020 年 5 月 22 日
This works for any array, 2D, 3D, etc., and returns the requested linear indices:
>> A = [0,0,0,0;0,0,0,0;1,0,4,8;2,0,5,9;3,1,6,7;0,2,7,0;0,0,0,0]
A =
0 0 0 0
0 0 0 0
1 0 4 8
2 0 5 9
3 1 6 7
0 2 7 0
0 0 0 0
>> S = size(A);
>> S(1) = 2;
>> X = A~=0;
>> X = X & (cumsum(X,1)==1 | flipud(cumsum(flipud(X),1))==1);
>> B = reshape(find(X),S)
B =
3 12 17 24
5 13 20 26
  5 件のコメント
Nicole Wan
Nicole Wan 2022 年 3 月 25 日
What if there are some columns before and after this data set that are columns of zero but the positions in the matrix need to be kept.
Stephen23
Stephen23 2022 年 3 月 25 日
A = [0,0,0,0;0,0,0,0;0,0,4,8;0,0,5,9;0,1,6,7;0,2,7,0;0,0,0,0]
A = 7×4
0 0 0 0 0 0 0 0 0 0 4 8 0 0 5 9 0 1 6 7 0 2 7 0 0 0 0 0
X = A~=0;
X = X & (cumsum(X,1)==1 | flipud(cumsum(flipud(X),1))==1)
X = 7×4 logical array
0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 0 1 1 0 0 0 0 0
B = reshape(find(X),2,[])
B = 2×3
12 17 24 13 20 26

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


Matt J
Matt J 2022 年 3 月 25 日
編集済み: Matt J 2022 年 3 月 25 日
A=[0 0 0 0
0 0 0 0
1 0 4 0
2 0 5 0
3 1 6 0
0 2 7 0
0 0 0 0];
a=logical(A);
[~,first]=max(a,[],1,'linear');
[~,last]=min( cummax(a,1,'rev'), [],1,'linear');
B=[first;last-1]
B = 2×4
3 12 17 22 5 13 20 21
B(:,~any(A,1))=nan
B = 2×4
3 12 17 NaN 5 13 20 NaN
  2 件のコメント
Bruno Luong
Bruno Luong 2022 年 3 月 25 日
I believe this method fails if a column contains only 0s.
Matt J
Matt J 2022 年 3 月 25 日
It's easily tweaked to accomodate that case (I've now done so).

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


Bruno Luong
Bruno Luong 2022 年 3 月 25 日
Let you do conversion to linear index
A=[0 0 0 0
0 0 0 0
1 0 4 8
2 0 5 9
3 1 6 7
0 2 7 0
0 0 0 0]
A = 7×4
0 0 0 0 0 0 0 0 1 0 4 8 2 0 5 9 3 1 6 7 0 2 7 0 0 0 0 0
[r,c]=find(A);
first=accumarray(c(:),r(:),[size(A,2) 1], @min, 0)
first = 4×1
3 5 3 3
last=accumarray(c(:),r(:),[size(A,2) 1], @max, 0)
last = 4×1
5 6 6 5

Bruno Luong
Bruno Luong 2022 年 3 月 25 日

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by