Breaking down a vector into multiple vectors based on conditional changes

10 visualizaciones (últimos 30 días)
I have very large data files that need to be broken down based on conditions where data changes. See attached picture. The data changes somewhat predictably where the vector must be broken into new variables.
As another example, I started with this:
% Here's an example of what needs to happen
% the output variable #1:
a = 1.001:1:4.001;
% the output variable #2:
b = 1.002:1:4.002;
% the output variable I want #3:
c = 1.003:1:4.003;
% concatenate the variables to make them look like my data file (one long
% array of information that needs to be broken down). If this was my real data file, I would just import that data file rather than make the example "a", "b" and "c" vectors.:
d = [a,b,c];
l = length(d);
for ii = 1:l
% set a condition for when the data changes, make a new variable.
% I.e., when the data changes from -1 to -10, then the new variable
% should start.
if abs(d(ii+1) - d(ii)) > 1;
% then make a new output variable that should start with -10
bb = d(ii)
% now I dont' know where to go from here to keep adding numbers
% into bb (output variable #2). I also don't know how to
else
% if the change between indices was not bigger than the threshold,
% then keep building the first output variable.
aa = a(ii);
% the problem with this is that bb doesn't keep building, and you
% start building up aa again, even though you should be building a
% new variable here.

Respuesta aceptada

noMathWiz
noMathWiz el 13 de Jul. de 2020
Editada: noMathWiz el 13 de Jul. de 2020
I solved it myself, brute force method. In my case, I know the number of output variables I need, so the solution is a series of while loops based on a thresholding condition. In this case, example, I know that I want a new output variable as the data changes from 4.00__ to 1.00__. In otherwords, this si a jump of greater than two. Within each variable, changes in the data are less than two.
clear all
clc
% the data for variable one #1:
a = 1.001:1:4.001;
% the data for variable #2:
b = 1.002:1:4.002;
% the data for variable I want #3:
c = 1.003:1:4.003;
% concatenate the variables to make them look like my data file (one long
% array of information that needs to be broken down). If this was the real
% real data file, I would just import that data file rather than make the
% example "a", "b" and "c" vectors.:
data = [a,b,c,0]
% create indexing variable
ii = 1;
% Fill in data for the first output variable
% unless there is a big change in the data (thresholding condition), add
% the values in your data array to your first variable:
while abs(data(ii+1) - data(ii)) < 2 % condition for each indice change
aa(ii) = data(ii); % fill in the aa variable
ii = ii + 1; % increment counter
end
% if the above while condition is not true, then we've reached the last
% index of the first variable, but we didn't fill in this last index into
% our new variable yet.
aa(ii) = data(ii); % fill in the last value for the first variable
ii = ii + 1; % increment the counter
% Fill in data for the second variable
% set an "offsetting" index
n = ii;
while abs(data(ii+1) - data(ii)) < 2
jj = (ii-n) + 1; %offset the counter by your offsetting variable 'n'
bb(jj) = data(ii); % the index of your new variable will is restored
ii = ii + 1; % increment counter
end
% fill in the last value for the second output variable:
jj = (ii-n) + 1;
bb(jj) = data(ii);
ii = ii + 1; % increment the counter
% Fill in data for the third output variable
n = ii;
while abs(data(ii+1) - data(ii)) < 2
jj = (ii-n) + 1;
cc(jj) = data(ii);
ii = ii + 1; % increment counter
end
% print the output variables aa, bb, and cc to the workspace
aa
bb
jj = (ii-n) + 1;
cc(jj) = data(ii)

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by