how do I apply an if statement to each row of a column vector?
    조회 수: 6 (최근 30일)
  
       이전 댓글 표시
    
i have a 1x10 column vector consisting of random numbers. 
and i want to apply the following if statement to each number in the column vector. 
  if R < 0.5;
    step = -1; 
elseif R > 0.5; 
    step = 1;  
  end
where R is the column vector, and so the solution would be a 1x10 column vector consisting of either +1 or -1 
any help is appreciatied, thanks. 
댓글 수: 0
채택된 답변
  Sudhakar Shinde
      
 2020년 10월 15일
        
      편집: Sudhakar Shinde
      
 2020년 10월 15일
  
      Try this:
R = [0.1:0.1:1];
step=zeros(1,length(R(:)));
for n=1:length(R(:))
    if R(n)<0.5
        step(n)= -1;
    elseif R(n) > 0.5
        step(n) = 1;
    end  
end
댓글 수: 2
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

