Clear Filters
Clear Filters

How to add leading zero to a letter in matlab

3 views (last 30 days)
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 Comments
Jan
Jan on 27 Aug 2017
Edited: Jan on 27 Aug 2017
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 on 17 Sep 2017
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.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 27 Aug 2017
Edited: Stephen23 on 27 Aug 2017
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 Comments
Stephen23
Stephen23 on 4 Jun 2018
Edited: Stephen23 on 4 Jun 2018
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 on 9 Jun 2019
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!