adding counts of ordered pairs
이전 댓글 표시
I have a sequence of
by 3 arrays, say
,
, …,
that are generated within a loop. That is, the number of rows of each array varies, but each array has three columns. The first two columns represent ordered pairs. The third column is the count of those ordered pairs. I want to "add" these together so that I get an array that accumulates the counts of these ordered pairs into a new array that is
with the same structure. For example, if
is
by 3 arrays, say
,
, …,
isA1=[1 4 3;
3 5 1;
12 4 7;
13 5 2;
14 1 1];
and
is
isA1=[1 5 1;
13 5 1;
13 7 3;
14 1 5];
then the cumulative matrix should be
A=[1 4 3;
1 5 1;
3 5 1;
12 4 7;
13 5 3;
13 7 3;
14 1 6];
The arrays being "summed" in this way have hundreds of entries and are themselves summaries of arrays with thousands of entries, so efficiency matters.
채택된 답변
추가 답변 (1개)
A1=[1 4 3;
3 5 1;
12 4 7;
13 5 2;
14 1 1];
A2=[1 5 1;
13 5 1;
13 7 3;
14 1 5];
A12=[A1; A2];
[A12u,~,J]=unique(A12(:,1:2),'rows','stable');
A=[A12u,accumarray(J,A12(:,3))]
댓글 수: 7
Barbara Margolius
2022년 9월 6일
편집: Barbara Margolius
2022년 9월 6일
Bruno Luong
2022년 9월 6일
편집: Bruno Luong
2022년 9월 6일
I hope you don't have such case where Bjorn's method fails
A1=[1 1 1];
A2=[1 1 -1];
or pairs are not integer >= 1.
Barbara Margolius
2022년 9월 6일
편집: Barbara Margolius
2022년 9월 6일
Bruno Luong
2022년 9월 6일
floor can returns 0 no?
Barbara Margolius
2022년 9월 6일
Bjorn Gustavsson
2022년 9월 6일
@Bruno Luong: The amount of subconsious/implicit assumptions I make when writing QD-solutions is a bit frightening.
Bruno Luong
2022년 9월 6일
That's called "intuition".
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!