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

4 visualizzazioni (ultimi 30 giorni)
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,

Risposta accettata

Bruno Luong
Bruno Luong il 29 Ago 2022
Modificato: Bruno Luong il 29 Ago 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

Più risposte (3)

Karim
Karim il 29 Ago 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

Torsten
Torsten il 29 Ago 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

dpb
dpb il 29 Ago 2022
Modificato: dpb il 29 Ago 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 Commenti
Syed Muhammad Ali
Syed Muhammad Ali il 30 Ago 2022
However, all the other solutions are also right.....
dpb
dpb il 30 Ago 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! :)

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by