How to find '1' for the second time in a matrix by row wise
4 ビュー (過去 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,
0 件のコメント
採用された回答
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)
% if you prefer an array of col2n1 alone indexed by row
col2n1 = accumarray(row, col2n1, [size(A,1),1])
その他の回答 (3 件)
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
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
0 件のコメント
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
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! :)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!