MATLAB Answers

Find and Replace matching values in two arrays corresponding to another array

3 views (last 30 days)
Zachary Kelley
Zachary Kelley on 26 Apr 2017
Edited: Will Nitsch on 1 May 2017
Hi! I'm trying to write a code that will compare "possibleadductpeaks' to the other two arrays and if the values do not match it will either say 0 or something like "no match".
I want the output for this particular set of data to say:
mzTemplateStrand=
0 0 0
mzComplementStrand=
3493 0 6640
______________________________________________________________________________
mzTemplateStrand=[3444,5452,6742];
mzComplementStrand=[3493,5452,6640];
PossibleAdductPeaks=[3493,6640];
%length of template strand
zz=length(mzTemplateStrand);
%length of complement strand
kk=length(mzComplementStrand);
%length of mzadduct matches
dd=length(PossibleAdductPeaks);
newStr=mzTemplateStrand
newStr2=mzComplementStrand
for i=1:zz; %1 to the end of template (1-3)
j=1:kk; %1 to the end of complement (1-3)
bb=1:dd; %1 to 2
if PossibleAdductPeaks(bb)==mzTemplateStrand(i)
newStr = strrep(mzTemplateStrand,i,0)
if PossibleAdductPeaks(bb)==mzComplementStrand(j)
newStr2 = strrep(mzComplementStrand,j,0)
end
end
end

Answers (1)

Will Nitsch
Will Nitsch on 1 May 2017
Edited: Will Nitsch on 1 May 2017
Pretty sure this is doing what you are looking for. The 'find' function is useful here. I stored the current index of 'PossibleAdductPeaks' being compared in 'idx(i)', and the corresponding index in matches_mzXXXX (either) is the index in that matrix that matches the ith value in PossibelAdductPeaks. I used cells in case for instance you had two matching values instead of one. That wouldn't happen in this case but you can now generalize to that if you wanted. Otherwise, normal arrays would be fine. Using this code, if you know there is a match (or isn't one), you can use 'fprintf' or 'disp' to output the string of your choice.
mzTemplateStrand=[3444,5452,6742];
mzComplementStrand=[3493,5452,6640];
PossibleAdductPeaks=[3493,6640];
% I used cells so you could generalize this to other cases, where you might
% get two or more matching indexes for a particular value in
% PossibleAdductPeaks
idx = [];
matches_mzComp = {[]};
matches_mzTemp = {[]};
for i = 1:length(PossibleAdductPeaks)
idx(i) = i; % Current index of PossibleAdductPeaks to compare to below outputs
matches_mzComp{i} = find(mzComplementStrand==PossibleAdductPeaks(i));
matches_mzTemp{i} = find(mzTemplateStrand==PossibleAdductPeaks(i));
end

Community Treasure Hunt

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

Start Hunting!