How to find column where 0 turns to 1 for each row
    조회 수: 6 (최근 30일)
  
       이전 댓글 표시
    
Hello!
I would like to be able to find the column (or column title) for each row where the 0 turns to 1. 
I have attached example rows (egcolumnsep.mat), so for example for row 5, I would like it to return column 20 (or Bins_11). I would like an output for all the rows though. Some rows might have only 0's (this can be NaN). This basically gives me the reaction time for each row. 
Thank you for your help! =)
댓글 수: 0
채택된 답변
  Bruno Luong
      
      
 2022년 7월 15일
        
      편집: Bruno Luong
      
      
 2022년 7월 15일
  
      load('egcolumnsep.mat');
c1 = find(strcmp(DATAb1.Properties.VariableNames,'Bins_1')); % == 10
% Extract relevant data
A = table2array(DATAb1(:,c1:end));
if ~all(ismember(A,[0 1]),'all')
    error('Data must be 0 or 1')
end
[cols,rows] = find(diff(A.')==1);
cols = cols+c1;
Idx01 = table(rows,cols)
댓글 수: 2
  Bruno Luong
      
      
 2022년 7월 15일
				
      편집: Bruno Luong
      
      
 2022년 7월 15일
  
			Why didn't tell us that you are interested only the first transition in the question?
You can call this statement after the above scriot
groupsummary(Idx01, 'rows','min')
or start from scratch
load('egcolumnsep.mat');
c1 = find(strcmp(DATAb1.Properties.VariableNames,'Bins_1')); % == 10
% Extract relevant data
A = table2array(DATAb1(:,c1:end));
if ~all(ismember(A,[0 1]),'all')
    error('Data must be 0 or 1')
end
[a,cols] = max(A,[],2);
cols = (cols+c1-1) .* (a==1) % returns 0 if no transition 0->1 is found
추가 답변 (1개)
  Jonas
      
 2022년 7월 14일
        
      편집: Jonas
      
 2022년 7월 14일
  
      yourMat=[0 0 0 0 0;
    0 1 1 0 0;
    0 0 0 1 0];
where=arrayfun(@(idx)find(diff(yourMat(idx,:))==1,1,'first')+1,1:size(yourMat,1),'UniformOutput',false)'
an empty cell shows that there was not such a change
you could set it to NaN and convert to an normal array by
where{cellfun('isempty',where)}=NaN
where=cell2mat(where)
댓글 수: 3
  Jonas
      
 2022년 7월 14일
				you can convert your table to a matrix before, e.g. yourTable{:,:} or table2array()
참고 항목
카테고리
				Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


