Sum an array in one dimension to calculate the probability
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi,
I have an array1 (4x3x5) of probabilities. I constructed a second array2 corresponding of probabilities to find some values,for each value in array2 I look in the array1 of probabilies at the same index to know the probability to find the value.
Now the problem is that, in array2, some of the values are duplicated. So first I have to remove all the duplicated values, and to recalculate the probabilities of the new unique values by calculating the sum of the probabilities in the second column where the value is the same in array1.
This is all what I find :
uniqueC = unique(C); to calculate the unique elements of the array. I tried to do manipulations like this : n = histc(C(:), uniqueC, 1);
setdiff(C(:), uniqueC);
But it doesn't work with (4x3x5).
I searched for days before I came here.
Please help me, I need it very soon for an assignement.
댓글 수: 4
Babak
2013년 5월 7일
if you want to remove the duplicate, what are you going to replace it with? what's the problem with duplication? I don't really understand. Please give an example of a sample C array that is undesirable and also say how you want to make it desirable so we suggest comments on how you do it.
채택된 답변
Iman Ansari
2013년 5월 8일
Hi.
A(:,:,1) =[0.4528 0.0477 0.0715;0.0220 0.0023 0.0035;0.0045 0.0005 0.0007;0.0511 0.0054 0.0081];
A(:,:,2) =[0.0129 0.0014 0.0020;0.0349 0.0037 0.0055;0.0188 0.0020 0.0030;0.0052 0.0005 0.0008];
A(:,:,3) =[0.0065 0.0007 0.0010;0.0013 0.0001 0.0002;0.0123 0.0013 0.0019;0.0239 0.0025 0.0038];
A(:,:,4) =[0.0194 0.0020 0.0031;0.0078 0.0008 0.0012;0.0446 0.0047 0.0070;0.0427 0.0045 0.0067];
A(:,:,5) =[0.0032 0.0003 0.0005;0.0058 0.0006 0.0009;0.0149 0.0016 0.0023;0.0071 0.0007 0.0011];
C(:,:,1) =[0 25000 35000;20000 45000 55000;
60000 85000 95000;50000 75000 85000];
C(:,:,2) =[15000 40000 50000; 35000 60000 70000
75000 100000 110000; 65000 90000 100000];
C(:,:,3) =[30000 55000 65000;50000 75000 85000
90000 115000 125000;80000 105000 115000];
C(:,:,4) =[75000 100000 110000;95000 120000 130000
135000 160000 170000; 125000 150000 160000];
C(:,:,5) =[5000 30000 40000;25000 50000 60000
65000 90000 100000;55000 80000 90000];
C_1=C(:);
C_2=unique(C_1);
C_3=zeros(size(C_2));
for i=1:length(C_2)
idx=find(C==C_2(i));
C_3(i)=sum(A(idx));
end
C_3
댓글 수: 0
추가 답변 (1개)
Austin
2013년 5월 8일
So to get this straight, if you have more than one of the same value (lets say 50000), then you must add up the sum of the percentages corresponding to it? If this is the case, then it might be easier to think about it you put all probabilities into one large column, and all the corresponding values into another large column. This will make it easier to find all duplicate values. This also makes it easier to delete the multiple values, because you don't have to worry about keeping the same number of rows and columns in a matrix.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!