Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Sum an array in one dimension to calculate the probability

Asked by mercury on 7 May 2013

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 Comments

mercury on 7 May 2013

for example in array2 I have :

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

... etc.

The value 50000 appears more than one time, each time it has different probability in array1.

Babak on 7 May 2013

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.

mercury on 7 May 2013

The duplication is not good beacause I have to build a new array of probability law for a random variable, the values of the variable are in array2 and the probabilities are in array1. Here is array1

(:,:,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

(:,:,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

(:,:,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

(:,:,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

(:,:,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

And array 2 :

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

I have to built an array3 like this (this is done):

   0
        5000
       15000
       20000
       25000
       30000
       35000
       40000
       45000
       50000
       55000
       60000
       65000
       70000
       75000
       80000
       85000
       90000
       95000
      100000
      105000
      110000
      115000
      120000
      125000
      130000
      135000
      150000
      160000
      170000

And the array4 (the probability low of array3) will for each indice in array3, calculate the probability like this (*the result is an array with the same dimension as that of array3*) :

0.4528 //zero apears one time, we looked for the first indice ... 0,055 //at the indice of 50000 ...

for example 50000 appears in 4 indices in array2, I cheked the values at these indices in array1 and did the sum to find the total probability : 0.0511 + 0.0020 + 0.0006 + 0.0013 = 0,055

Thank you

mercury

Products

No products are associated with this question.

2 Answers

Answer by Iman Ansari on 8 May 2013
Accepted answer

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 Comments

Iman Ansari
Answer by Austin on 8 May 2013

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 Comments

Austin

Contact us