Using vectorization, how do I reduce a vector to only keep 1 instance of a repeating value (not sorted).
Mostrar comentarios más antiguos
Hello,
I am trying to filter recorded data. I have a structure of all my DAQ channels, and each field is a channel. For each field, it is an array with the saved measurements. I want to plot the measurements, but not over time. I want to look at only when the value changes. My problem is that I want to make a smaller vector that just has the updated numbers in it, so that I can plot it later.
I'm not good with vectorization, but I don't think unique or diff can quite be used for this. Either way, I'm not very experienced with Matlab's functions (so what do I know?). I am assuming that this kind of task can be optimized by using some kind of logical indexing instead of a for loop.
For example:
cnt = [1;1;1;1;1;2;2;3;2;1;-1;....]
Updates = [1;2;3;2;1;-1;..]
I created the following code:
for i = 1: ChannelCount
DataName = (genvarname(char(ChannelNames{i,1})));
Data = strtRawChannel.(DataName);
DataLength = length(Data);
UpdateHolder(DataLength,1) = 0;
k = 1;
UpdateHolder(k) = Data(1);
for j = 1:(DataLength-1)
if ((Data(j+1)-Data(j))~=0)
k = k + 1;
UpdateHolder(k) = Data(j+1);
end
end
FilteredData.(DataName) = UpdateHolder(1:k);
end
Is there any hope to improving this code? It's not so bad for (300000,1), but I rather not be messy about this coding.
Thank you,
Kyle
Respuesta aceptada
Más respuestas (1)
Azzi Abdelmalek
el 19 de En. de 2013
Editada: Azzi Abdelmalek
el 19 de En. de 2013
cnt(logical([1; diff(cnt)]))
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!