How to find '1' for the second time in a matrix by row wise

4 ビュー (過去 30 日間)
Syed Muhammad Ali
Syed Muhammad Ali 2022 年 8 月 29 日
コメント済み: dpb 2022 年 8 月 30 日
Hi,
I have a matrix A , in which I am looking for '1' as appearing for the second time (using row wise).
A = [ 0 0 1 0 0 0 0 0 1 1 0 0; 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0 1 0];
In the first row of matrix above, '1' is appearing at index 3 and at index 9.
If '1' is not appearing for second time or '1' is not available in the row, it should give the answer of NaN or 0.
The answer could be in a column vector, like c = [9;NaN;11] for the matrix A above.
Thanks,

採用された回答

Bruno Luong
Bruno Luong 2022 年 8 月 29 日
編集済み: Bruno Luong 2022 年 8 月 29 日
A = [ 0 0 1 0 0 0 0 0 1 1 0 0; 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0 1 0];
At = A.';
[col2n1,row]=find(cumsum(At)==2 & At==1);
table(row,col2n1)
ans = 2×2 table
row col2n1 ___ ______ 1 9 3 11
% if you prefer an array of col2n1 alone indexed by row
col2n1 = accumarray(row, col2n1, [size(A,1),1])
col2n1 = 3×1
9 0 11
  1 件のコメント
dpb
dpb 2022 年 8 月 29 日
Nicely done as usual, Bruno!!! :)

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

その他の回答 (3 件)

Karim
Karim 2022 年 8 月 29 日
If you don't mind using a loop, you can do this the follwing way:
A = [ 0 0 1 0 0 0 0 0 1 1 0 0; 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0 1 0];
% find all ones in the matrix
[row,col] = find(A == 1);
% initialize an empty nan vector
c = nan(size(A,1),1);
% loop over the rows's of A
for i = 1:size(A,1)
% look at the number of ones (if any) at the current row
col_idx = col( row == i );
% make sure there are more then 2 ones
if numel(col_idx)>=2
% pick the index of the second 'one' and store it in c
c(i) = col_idx(2);
end
end
c
c = 3×1
9 NaN 11
  1 件のコメント
Image Analyst
Image Analyst 2022 年 8 月 29 日
+1 vote for adding very helpful comments! 🙂 👍

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


Torsten
Torsten 2022 年 8 月 29 日
A = [ 0 0 1 0 0 0 0 0 1 1 0 0; 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 1 0 1 0];
c = zeros(size(A,1),1);
for i = 1:size(A,1)
k = find(A(i,:),2);
if numel(k) < 2
c(i) = NaN;
else
c(i) = k(2);
end
end
c
c = 3×1
9 NaN 11

dpb
dpb 2022 年 8 月 29 日
編集済み: dpb 2022 年 8 月 29 日
Probably more efficient way to deal with the cell array indexing, but
C=arrayfun(@(i)find(A(i,:),2),1:size(A,1),'UniformOutput',false).';
ix=cellfun(@isempty,C);
C(ix)={nan(1,2)};
C=cell2mat(C);
C=C(:,2);
ADDENDUM: Handle the one "1" case as well and clean up addressing a little on the way...
C=arrayfun(@(i)find(A(i,:),2),1:size(A,1),'UniformOutput',false).'; % locations found or empty by row
ix=cellfun(@(c)numel(c)<2,C); % those that had none or one (<2)
C(ix)={nan(1,2)}; % fixup so all cells have precisely 2 elements
C=cellfun(@(c)c(end),C); % and return that second index (NaN if <2 found)
One would have to have two indices to differentiate between empty and one occurrence if that were to be important -- but then would also have to have a way to code that a finite value returned is the one and only 1 found instead of second -- changing sign there to return negative value would be about only way in the same storage footprint.
  12 件のコメント
Syed Muhammad Ali
Syed Muhammad Ali 2022 年 8 月 30 日
However, all the other solutions are also right.....
dpb
dpb 2022 年 8 月 30 日
But Bruno's is the most efficient by far, which is to be preferred -- although I do think my fixup is kinda' a cute workaround! :)

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

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by