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

3 次查看(过去 30 天)
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

回答(1 个)

Will Nitsch
Will Nitsch 2017-5-1
编辑:Will Nitsch 2017-5-1
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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by