How to group numbers of a vector
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I have the following vector:
V = [1 2 3 5 7 9 11 13 14 15 16 17 20 22 24 26]
I would like to group the numbers according to their difference (step value) from each other so they look like:
Group 1 = [1 2 3]
Group 2 = [5 7 9 11 13]
Group 3 = [14 15 16 17]
Group 4 = [20 22 24 26]
Could anyone help me to sort out this?
Thank you!
6 个评论
Stephen23
2018-9-27
编辑:Stephen23
2018-9-27
" Group 1 and 5 are incorrect as it is seen 339 should go in group 2 ..."
It is not enough to simply write that some groups "... are incorrect", because you have not told us anywhere what the algorithm is that you use to decide this. Image Analyst already asked you to clarify this. You stated that "...but the actual vectors that I work with consist only odd or even numbers, which makes the discrimination easier", but this does not explain 3 in your original example, and the number 339 in your comment above, both of which could fit into either group perfectly. You need to tell us exactly how to decide which group they fit into.
采纳的回答
更多回答(2 个)
Bruno Luong
2018-9-26
OK, try this:
b = [true, false, diff(V,2)~=0];
b = b & [true, ~b(1:end-1)];
b = find(b);
lgt = [b(2:end), length(V)+1] - b;
G = mat2cell(V,1,lgt);
G{:}
Bruno Luong
2018-9-28
编辑:Bruno Luong
2018-9-28
Can you try this one, I put the flag so that you can select if the common point that goes to left or right group, if you have other specific rule you need to specify it.
V = [315 339 341 343 345 347 349 351 ...
353 355 357 359 361 363 365 371 ...
373 375 377 379 381 387 389 391 ...
393 395 397 399 401 403 405 407 ...
409 411 413 415 417 419 421 423 ...
425 427 429 431 433 435 437 439 ...
441 443 445 447 449 473 727 729 ...
731 733 735 737 739 745 747]
GoLeftFlag = false; % choice of the common point goes to the left or right group
n = length(V);
d = diff([true, diff(V,2)==0, true]);
s = [1, find(d==1)];
e = [find(d==-1)+1, n];
collision = [0 e(1:end-1)]>=s;
if GoLeftFlag
s(collision) = s(collision) + 1;
else
collision(1) = [];
e(collision) = e(collision) - 1;
end
i2 = arrayfun(@(s,e) min(bsxfun(@plus,s:2:e,[0;1]),e), ...
e(1:end-1)+1, s(2:end)-1, 'unif', 0);
c = cell(1,2*size(s,2)-1);
c(1:2:end) = num2cell([s; e],1);
c(2:2:end-1) = i2;
i = cat(2,c{:});
lgt = diff(i,1,1)+1;
G = mat2cell(V,1,lgt);
G{:}
9 个评论
Bruno Luong
2018-11-23
编辑:Bruno Luong
2018-11-23
OK I've modified the code to make group for only step <= 2. It looks ugly
V = [ 266 270 273 274 276 280 313 314 315 439 455 456 ...
457 471 472 473 474 475 476 863]
GoLeftFlag = false; % choice of the common point goes to the left or right group
n = length(V);
d = diff([true, diff(V,2)==0, true]); % & [true, c] & [c, true]);
s = [1, find(d==1)];
e = [find(d==-1)+1, n];
collision = [0 e(1:end-1)]>=s;
if GoLeftFlag
s(collision) = s(collision) + 1;
else
collision(1) = [];
e(collision) = e(collision) - 1;
end
i2 = arrayfun(@(s,e) min(bsxfun(@plus,s:2:e,[0;1]),e), ...
e(1:end-1)+1, s(2:end)-1, 'unif', 0);
c = cell(1,2*size(s,2)-1);
c(1:2:end) = num2cell([s; e],1);
c(2:2:end-1) = i2;
i = cat(2,c{:})';
% Make group only if step is <= 2
dv = diff(V(i),1,2)./diff(i,1,2);
stepbig = dv(:)>2;
s = [n,1];
up = accumarray(i(stepbig,1),1,s);
down = -accumarray(i(stepbig(1:end-1),2)+1,1,s);
g = accumarray(i(~stepbig,1),1,s);
lgt = diff(find([cumsum(up+down)+g; true]));
G = mat2cell(V,1,lgt);
G{:}
The result is
G{1} = 266
G{2} = 270
G{3} = 273 274
G{4} = 276
G{5} = 280
G{6} = 313 314 315
G{7} = 439
G{8} = 455 456 457
G{9} = 471 472 473 474 475
G{10} = 476
G{11} = 863
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!