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

3 vues (au cours des 30 derniers jours)
Syed Muhammad Ali le 29 Août 2022
Commenté : dpb le 30 Août 2022
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,
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Bruno Luong le 29 Août 2022
Modifié(e) : Bruno Luong le 29 Août 2022
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 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
dpb le 29 Août 2022
Nicely done as usual, Bruno!!! :)

Connectez-vous pour commenter.

### Plus de réponses (3)

Karim le 29 Août 2022
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 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Image Analyst le 29 Août 2022

Connectez-vous pour commenter.

Torsten le 29 Août 2022
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
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

dpb le 29 Août 2022
Modifié(e) : dpb le 29 Août 2022
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 commentairesAfficher 10 commentaires plus anciensMasquer 10 commentaires plus anciens
Syed Muhammad Ali le 30 Août 2022
However, all the other solutions are also right.....
dpb le 30 Août 2022
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! :)

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Programming dans Help Center et File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by