Combine and sorting array values
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello!
I got a result matrix A
A = [3 9 4]
And another matrix B
[ 3 8
4 8
15 5
B= 16 1
16 2
16 7
17 6
17 2 ]
First i want to combine A with all values of the second column of B that have a unique value in the first column -> Matrix AB
AB = [3 9 4 8 5]
As a final result i want the combinations of AB with all values of the second column of B that have not a unique value in the first column ->Result
Result= [3 9 4 8 5 1 6 3 9 4 8 5 1 2
3 9 4 8 5 2 6 3 9 4 8 5 2 2
3 9 4 8 5 7 6 3 9 4 8 5 7 2]
How can i do that?
Many thanks in advance!
채택된 답변
Maybe something like:
A = [3 9 4];
B = [3 8
4 8
15 5
16 1
16 2
16 7
17 6
17 2];
U = unique(B(:,1));
c = histcounts(B(:,1),U);
col1unique = ismember(B(:,1),U(c==1));
AB = [A unique(B(col1unique,2),'stable').']
AB = 1×5
3 9 4 8 5
combpool = unique(B(~col1unique,2));
combs = nchoosek(combpool,2);
result = [repmat(AB,[size(combs,1) 1]) combs]
result = 6×7
3 9 4 8 5 1 2
3 9 4 8 5 1 6
3 9 4 8 5 1 7
3 9 4 8 5 2 6
3 9 4 8 5 2 7
3 9 4 8 5 6 7
If you want the result formatted as shown, then:
result = reshape(result.',size(result,2)*2,[]).'
result = 3×14
3 9 4 8 5 1 2 3 9 4 8 5 1 6
3 9 4 8 5 1 7 3 9 4 8 5 2 6
3 9 4 8 5 2 7 3 9 4 8 5 6 7
댓글 수: 6
Thanks! Works for the example, but if B is
B = [3 8
4 8
15 5
16 1
16 2
16 7]
then i get AB without the "5"
AB = 1 2 3 8
I don't clearly get it why..
And the result combinations are also wrong..
It's like the values of 16 are in one group and the values of 17 are in another group.
Now i want all combinations of group 16 with group 17..
Would be great if you can help me!! Thanks a lot!
Auugh. I should've seen that coming. I wrote that using the old histc() and just substituted the newer histcounts() and ignored how it behaves differently on the ends. I'm just going to stick with histc() instead of trying to set up bins for histcounts().
A = [3 9 4];
B = [3 8
4 8
15 5
16 1
16 2
16 7];
U = unique(B(:,1));
c = histc(B(:,1),U); %#ok<HISTC>
col1unique = ismember(B(:,1),U(c==1));
AB = [A unique(B(col1unique,2),'stable').']
AB = 1×5
3 9 4 8 5
Everything else should be the same
combpool = unique(B(~col1unique,2));
combs = nchoosek(combpool,2);
result = [repmat(AB,[size(combs,1) 1]) combs]
result = 3×7
3 9 4 8 5 1 2
3 9 4 8 5 1 7
3 9 4 8 5 2 7
EDIT: I went ahead and wrangled a histcounts() solution anyway
A = [3 9 4];
B = [3 8
4 8
15 5
16 1
16 2
16 7];
[c ed] = histcounts(B(:,1),'binmethod','integers');
col1unique = ismember(B(:,1),ed(c==1)+0.5);
AB = [A unique(B(col1unique,2),'stable').']
AB = 1×5
3 9 4 8 5
Perfect thanks!
Still didn't get the last step yet..thought about to store all values of the second column which have
the same value in the first column in one cell and then get all combinations of them.
C = B(~col1unique, :)
C =
16 1
16 2
16 7
17 6
17 2
Combinations wanted
CC = [1 6; 1 2; 2 6; 2 2; 7 6; 7 2]
So at the end i can combine AB with CC..
Any idea?
Oh I misunderstood what kind of combinations you were after. I was just doing combinations of all remaining (unique) elements of col2.
This does combinations of the groups within col2 associated with the nonunique blocks in col1
A = [3 9 4];
B = [3 8
4 8
15 5
16 1
16 2
16 7
17 6
17 2];
[c ed] = histcounts(B(:,1),'binmethod','integers');
col1unique = ismember(B(:,1),ed(c==1)+0.5);
AB = [A unique(B(col1unique,2),'stable').']
AB = 1×5
3 9 4 8 5
% split col2 of B into groups associated with nonunique elements of col1
Bnonu = B(~col1unique,:);
g = findgroups(Bnonu(:,1));
ngroups = max(g);
elements = cell(1,ngroups);
for k = 1:ngroups
elements{k} = Bnonu(g==k,2);
end
% find combinations of elements between groups
combinations = cell(1,ngroups); %set up the varargout result
[combinations{:}] = ndgrid(elements{:});
combinations = cellfun(@(x) x(:), combinations,'uniform',false);
result = [combinations{:}];
result = [repmat(AB,[size(result,1) 1]) result]
result = 6×7
3 9 4 8 5 1 6
3 9 4 8 5 2 6
3 9 4 8 5 7 6
3 9 4 8 5 1 2
3 9 4 8 5 2 2
3 9 4 8 5 7 2
based on this:
Many thanks!!!!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
