MATLAB Answers

logical operation on a column within a matrix

조회 수: 29(최근 30일)
Christopher 2014년 2월 28일
편집: lvn 2014년 2월 28일
I have the m-by-n matrix POS
I want to focus on a single column and perform an element-wise logical operation. For instance, turning all zero's to ones:
However, this does nothing. On the other hand, this works on the whole matrix:
But, obviously, I just want to perform this operation on column 2. What am I missing? I do not want another line of code since speed is very important here.
Even more confusing, the following works on the first column, but not on the second:
POS(POS(:,1)==1)=2; % works
POS(POS(:,2)==1)=2; % not working

채택된 답변

lvn 2014년 2월 28일
This should work:
  댓글 수: 2
lvn 2014년 2월 28일
gives you a vector (of 0's and 1's).
normally assumes 'filter' to be a matrix. This is why filter=(POS==0) gives you the expected answer. What you want is just to change the second column, hence you only want to target the second column:
This tells matlab to change only the second column, using the filter as argument. Note sure this is very clearly explained, but read more here

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by