How can I sort data in relation to a previous data set?

1 view (last 30 days)
I have a data set that produces a cell for each iteration of a for loop. Each cell contains an n*2 matrix with n between 0 and 6. I want to compare the data in the current iteration to the data in the previous iteration and sort it. An example of the array contained in each cell may be something like
vec(i)=[800 0.1; 816 0.2; 804 0.3]
vec(i-1) = [801 0.4; 805 0.5; 812 0.6; 817 0.7]
where i is just the loop index. What I want to do is to match each of the values of a(i) to the nearest value of a(i-1) by its first column, i.e. match 800 to 801, 810 to 812 and 804 to 805. I can use indexing to tell me which position in a(i-1) has the element closest to the value of a(i) that is being analysed using the following commands
for kk = DataPointsSpec+1:-1:2 % loop through each file in a folder
SpectrumData = PeakFit([FolderName '\' Direc(kk).name]); % only relevant thing this does is csvread
Wavelength=SpectrumData(2:end-1,1); %defines variables for the data in the files
Intensity=SpectrumData(2:end-1,2);
% peak fitting function to find the data I want to store in the cells
[pks locs w]=findpeaks(Intensity,'NPeaks',6,'MinPeakProminence',1*10^-3,'MinPeakDistance',4); % seems about the right parameters to only get the modes and no other noise peaks
lambda = Wavelength(locs); % wavelength of each peak
if isempty(lambda)
vec = zeros(1,2); % eliminates problems coming from data sets with no peaks
else
vec = [lambda,pks]; % Makes a matrix of peak position and height
end
vecarray{kk} = vec; % saves each iteration in a cell
if size(vecarray{kk},1)<size(vecarray{kk-1},1) % I expect the size of the previous matrix to be the same or larger than the current matrix
for i = 1:size(vecarray{kk},1)
val = vecarray{kk}(i,1); % selects each row of the first column in turn of the current data array
[~,indexToClosestMatch] = min(arrayfun(@(x)min(abs(x-val)),vecarray{kk-1}(:,1))); % finds row position of closest value in previous iteration to the selected value in the current iteration
end
else
disp('do match') % placeholder
end
end
Importantly, it is inherent in the data that there will never be more than one data point in vecarray{kk} matched to a single value in vecarray{kk-1} - they will each match to a different value. I then want to write a command that makes a new cell that is the size of vecarray{kk-1} but containing the values of vecarray{kk}. Because I'm looking at the condition where there is a size difference between the two matrices, the data in vecarray{kk} will be smaller than the array size. I want to position this data in a particular way in the cell. For each element of vecarray{ii}, I want its position to be the same as the position of its matched value in vecarray{kk-1}, e.g. for the two data sets included at the top, for the value 800 in vec(i), the code would return an index of 1. I then want 800 to appear in the first row of the first column of the new cell, and 0.1 to appear in the first row of the second column. The new matrix created from vec(i) and vec(i-1) should look like:
newvec(i) = [800 0.1; 804 0.3; 0 0; 817 0.7]
where the zeros are in the position of the unmatched value of vecarray{kk-1}. I can't work out a way to achieve this so any help would be appreciated!

Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!