How to split an array of integers into arrays of varying lengths where all the elements are close in value to each other?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Bruno Luong
2018년 11월 29일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!