I want to break my sample like this. Problem with my logic. need help

suppose my sample is GAATGCT. i take a size say 4. my output will be like this GAAT,AATG,ATGC,TGCT. when it reach the last position it will stop. it tried for loop but instead of getting this kind of output it gives me single char. i think there is a problem with my logic. can anyone help me. subsample='GAATGCT'; for n=subsample(1:(end-4+1)) for i=subsample(1:4) display(i) end end

댓글 수: 3

for easier reading,
subsample='GAATGCT';
for n=subsample(1:end)
for i=subsample(1:4)
display(i)
end
end
Jan
Jan 2015년 3월 4일
편집: Jan 2015년 3월 4일
@Tonmoy: I've formatted your code. "Output like this" is not exact enough to guess, what you want to achieve. Please explain exactly, what the code should do.
i have some datasample. i have to cut it such a way that it gives me same size of subsample. every time loop counter will increase and gives me same size of subsample starting from the the next char.

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

 채택된 답변

data = 'GAATGCT'
N = 4 ;
for k= 1:numel(data)-N
subsample = data(k:k+N) ;
disp(subsample)
end

댓글 수: 5

you are not using the for loops properly. How it is written should display only one item. Look at the documentation on for loops. But what is happening is that in the inside for loop you are displaying i. and i is looping through each entry of subsample. Here is another way to look at it how you've typed it.
subsample='GAATGCT';
for n=['G' 'A' 'A' 'T' 'G' 'C' 'T']
for i=['G' 'A' 'A' 'T']
display(i)
end
end
so lets see if we look at the first iteration for when n = 'G' and i = 'G' you'll display 'G'. then 'A' 'A' 'T' for when n = 'G'. then repeat for n = the other letters.
so you'd want to work with index locations. like Jos did. if you wanted to go this route then.... i'm not sure how to accomplish it this way.
thank you sir. i really appreciate your help.
i am just a beginner of matlab. thank you sir for pointing out my fault. I will definitely look into the documentation.code written above here that was actually i want. thank you again.
i change this code little bit to get my desire output.
data = 'GAATGCT'
N = 4 ;
for k= 1:numel(data)-N+1
subsample = data(k:k+N-1) ;
disp(subsample)
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 3월 4일

댓글:

2015년 3월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by