input a string into a vector when n=odd number
    조회 수: 8 (최근 30일)
  
       이전 댓글 표시
    
clear clc
for n=0:20
end x=[0:n]'
I need to have the word 'jump' replace every odd number in my vector
example
0
jump
2
jump
4
댓글 수: 0
답변 (2개)
  Laurent
      
 2013년 9월 13일
        
      편집: Laurent
      
 2013년 9월 13일
  
      Hi,
First, you can't just put strings into a vector with numbers, you will need to use a cell array, so you need to define your 'x' differently. For example:
x=[0:20]';
xcell=num2cell(x);
Then you can assign the string jump to all the odd values of x:
for ii=1:length(xcell)
  if mod(xcell{ii},2)==1
    xcell{ii}='jump';
  end
end
Just out of curiosity, for what purpose do you need this?
댓글 수: 0
  Andrei Bobrov
      
      
 2013년 9월 13일
        n = 20;
x=(0:n)';
xcell=num2cell(x);
xcell(rem(x,2) == 1) = {'jamp'};
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


