How to split an array of integers into arrays of varying lengths where all the elements are close in value to each other?
2 views (last 30 days)
Show older comments
A function I wrote (ThermoOptimzation) outputs two 15x999 matricies (G_exp, and G_model), representing thermodyanmic data over 15 temperatures and 999 compositions. I am trying to find where these two matricies are equal, but have to use a wide tolerance range or rounding in order to obtain at least a single value for each temperature point (row).
[G_exp, G_model] = ThermoOptimization(nVars, nIter);
G_exp = round(G_exp,3,'significant');
G_model = round(G_model,3,'significant');
for i = 1:length(G_exp(:,1))
ind = find(eq(G_exp(i,:),G_model(i,:)))
end
In doing this, I get multiple points for some rows. For example, at row 2:
ind =
2 3 4 998 999
This is clearly two distinct "groupings", an equality at composition (column) 2-4 and another at 998-999. I'd like to seperate ind into two sets, that I can use later on. I've tried a for loop and a while loop checking if the next element was less than 5 away from the current one, but I'm unsure what to do to split the array when the difference is greater than 5.
for j = 2:length(ind)
if (ind(1,j) - ind(1,j-1)) < 5
continue
else
end
end
0 Comments
Accepted Answer
Bruno Luong
on 29 Nov 2018
ind = [2 3 4 998 999]
lgt = diff(find(diff([-Inf, ind, Inf])>=5));
gind = mat2cell(ind,1,lgt);
results:
>> gind{:}
ans =
2 3 4
ans =
998 999
0 Comments
More Answers (0)
See Also
Categories
Find more on Multidimensional Arrays 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!