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

조회 수: 1(최근 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
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! :)

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by