필터 지우기
필터 지우기

How to add leading zero to a letter in matlab

조회 수: 3 (최근 30일)
SUBHAJIT KAR
SUBHAJIT KAR 2017년 8월 27일
댓글: SUBHAJIT KAR 2019년 6월 9일
I have written a code which will accepts a sequence and convert it to specific numerical sequence. The code is given below--
if true
% code
end
function[xA]=dnatobinary(sample_dna)
for i=1:length(sample_dna)
if sample_dna(i)=='A'|| sample_dna='a'
xA(i)=01;
elseif sample_dna(i)=='T' || sample_dna='t'
xA(i)=11;
end
end
end
Now this code should convert 'ATTTA' into '0111111101'. Instead it is converting it as '11111111' i.e the leading zero for A=01 is missing.please provide appropriate coding.
  댓글 수: 2
Jan
Jan 2017년 8월 27일
편집: Jan 2017년 8월 27일
This is not twitter: I've removed the # from the tags.
Note that leading zeros of numbers are meaningless and therefore ignored in Matlab. xA(i)=01 is exactly the same as xA(i)=1. If you need the leading zeros, stay at a character vector or use two numbers per element.
Cedric
Cedric 2017년 9월 17일
I think that you are confusing floating point doubles, binary codes, and strings. It seems that you want to convert a string into another string, made of '0' and '1' characters. For this I wouldn't use regular expressions but indexing or basic arithmetic, especially if you need to operate quickly on large sequences.

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

채택된 답변

Stephen23
Stephen23 2017년 8월 27일
편집: Stephen23 2017년 8월 27일
It is much simpler and more efficient to use ismember or regexprep:
>> str = 'ATTTA';
>> regexprep(str,{'A','T'},{'01','11'},'ignorecase')
ans = 0111111101
or
>> [~,idx] = ismember(upper(str),'AT');
>> C = {'01','11'};
>> [C{idx}]
ans = 0111111101
You will simply waste a lot of time trying to replicate functionality that already exists.
  댓글 수: 46
Stephen23
Stephen23 2018년 6월 4일
편집: Stephen23 2018년 6월 4일
Using ismember, as I showed in my answer, makes your code simpler and more efficient:
S = fastaread('sequence_AF099922.fasta');
V = S.Sequence(7021:15080); % your uploaded file uses title case.
R = [0.1260,0.1335,0.1340,0.0806];
[~,idx] = ismember(upper(V),'ATCG');
x = R(idx);
"But is is not generating the desired result."
As you did not explain what the "desired result" is, I can only advise you to debug your code:
SUBHAJIT KAR
SUBHAJIT KAR 2019년 6월 9일
Sir,
Can you please help me with sliding window DFT ? I want to find DFT of a sequence of 8000 points. I want to compute DFT of first 351 points then the window will be slided one pts and the DFT for the next window will be calculated untill the window reach the last point. I want to compute signal to noise ratio of window segment and plot it. I have written this code but it is plotting for the first 351 points.
for idx = 1:351:length(x)
slice = x(1+(idx-1):(idx-1)+351);
spectrum = fft(slice);
signal_noise = max(spectrum)/mean(spectrum);
end
But as the window slided through the length there should be 8000 signal to ratio points which would be plotted against the index. Please help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by