# How do I align two vectors similar to that done in plot?

조회 수: 17(최근 30일)
Jason Powell 2019년 6월 11일
편집: Guillaume Erny 2021년 3월 7일
I have two sets of corresponding vectors that I want to compare. The first set of vectors contain pixel intensity values for every frame Int(F), and the second set contain wavelengths of light (wvLngth(F)). From these vectors I want to produce a third vector that is pixel intensity in terms of wavelength Int(wvLngth). The problem is that vectors containing the wavelengths do not start and end at the same wavelength, nor do the wavelengths increase at the same rate.
For example:
int1Data = [1 2 3 4];
int2Data = [5 6 7 8];
wvLngth1Data = [7 9 10 11];
wvLngth2Data = [9 10 11 13];
%I can plot these two together to align them but how can I add them in the way they are plotted?
figure
plot(wvLngth1Data, int1Data);
hold on
plot(wvLngth2Data, int2Data);
%I'd like to produce something like:
intAvg = [1 3.5 4.5 5.5 8]
wvLngthComb = [7 9 10 11 13]
figure
plot(wvLngthComb, intAvg);
I've looked at things to align or interpolate the data, but I don't think I can align my data set just at the start, because the middle or end would be wrong, and I don't need Matlab to interperet what it things the missing points should be either.
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Jason Powell 2019년 6월 11일
By "int(F)" I mean to say: for frame x the intensity is y. So the relation I have (and I recognize is a stretch) is that the first element of all the vectors belongs to the same image, and the second to the second image, ect..
Regardless of the lack of a relationship; however, when I call plot on both sets of data, Matlab is able to align them (atleast visually) so I can see both sets of data on the same cordinate system. I'm wondering if it is somehow possible use that visual alignment mathmatically?
I suppose I could make a unique vector for each element in my data set containing every single possible wavelength, and then populate that vector with the data that I do have for each element, but I'm hoping there is a more clever/ built in way that I could call upon.
Thanks

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

### 채택된 답변

James Browne 2019년 6월 12일
Greetings,
I think I understand what you are after. I used your example data and wrote a script that creates a list of unique wavelength values from both wavelength data vectors, determines the average pixel intensity for each wavelength, assembles the averages in a vector which corrosponds to the vector of unique wavelength values and then plots the result. It would take quite some work to rework the code if you have more than two sets of intensity/wavelength data but it is certainly doable.
Here is what I came up with:
int1Data = [1 2 3 4];
int2Data = [5 6 7 8];
wvLngth1Data = [7 9 10 11];
wvLngth2Data = [9 10 11 13];
%Determine length of wave length data vectors
n = length(wvLngth2Data);
%Initialize count of mathces variable for use in recording indexes
%of matching wave length values and creating unique wavelength value vector
count1 = 1;
count2 = 1;
%Find unique wavelength values
waveTot = [wvLngth1Data,wvLngth2Data];
%Determine total number of wavelength values
nT = length(waveTot);
%Intialize variables to indicate duplicate has been found and if current
%value is a duplicate
dupFound = false;
duplicate = false;
%Find unique wavelength values
for i = 1:nT-1
for j = i+1:nT
if (waveTot(i) == waveTot(j) )
repeatIndices(count1) = j;
count1 = count1 +1;
dupFound = true;
end
end
if (dupFound)
for k = 1:count1 - 1
if ( i == repeatIndices(k) )
duplicate = true;
end
end
end
if(~duplicate)
uniqueWaveVals(count2) = waveTot(i);
count2 = count2 + 1;
end
if( (j == nT) )
duplicate = false;
for k = 1:count1 - 1
if ( i == repeatIndices(k) )
duplicate = true;
end
end
if( ~duplicate )
uniqueWaveVals(count2) = waveTot(j);
end
end
duplicate = false;
end
%Determine number of unique wave length values
nUniques = length(uniqueWaveVals);
%Initialize variables to indicate where a unique waveform appears
foundIn1 = false;
foundIn2 = false;
%Initiate variables for recoding the index at which unique wavelengths are
%found
match1 = 0;
match2 = 0;
%Preallocate memory to store average values
avgIntensities = zeros(1,nUniques);
%Calculate average values for each unique wave form value
for i = 1:nUniques
foundIn1 = false;
foundIn2 = false;
for j = 1:n
if (wvLngth1Data(j) == uniqueWaveVals(i) )
foundIn1 = true;
match1 = j;
end
end
for j = 1:n
if (wvLngth2Data(j) == uniqueWaveVals(i) )
foundIn2 = true;
match2 = j;
end
end
if(foundIn1 && ~foundIn2)
avgIntensities(i) = int1Data(match1);
end
if(~foundIn1 && foundIn2)
avgIntensities(i) = int2Data(match2);
end
if(foundIn1 && foundIn2)
avgIntensities(i) = (int2Data(match2)+int1Data(match1))/2;
end
end
plot(uniqueWaveVals,avgIntensities)
title('Average Pixel Intensity VS Light Wavelength')
xlabel('Wavelength (units)')
ylabel('Average Pixel Intensity (units)')
If this is not quite what you are looking for, I hope that it at least gives you a few good ideas!
##### 댓글 수: 1표시숨기기 없음
Jason Powell 2019년 6월 12일
Thanks James,
This is a great start to what I was thinking!!

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

### 추가 답변(1개)

Guillaume Erny 2021년 3월 7일
편집: Guillaume Erny 2021년 3월 7일
Hi Jason,
If you want to align two set of data to a common axis, you should look at the function interp1.
I always use this function when I want to compare spectroscopic date with different axes and it work perfectly

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

### Community Treasure Hunt

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

Start Hunting!