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

Adding Column values of a matrix

Asked by M@lik Ali on 6 Feb 2013

Hi, I have a matrix like below.

all_one_matrix(:,:,1)

ans =

Columns 1 through 8
    0.1979    0.1979    0.1354    0.0729    0.3542    0.3021    0.1771    0.0729
    0.4167    0.0938    0.2708    0.3958    0.0417    0.1042    0.3438    0.4583
    0.1250    0.1771    0.1354    0.1146    0.0938    0.1771    0.1771    0.1042
    0.1563    0.1146    0.1563    0.1667    0.1458    0.0833    0.1354    0.1146
    0.0417    0.0521    0.0521    0.0729         0    0.0625    0.1250    0.1250
Columns 9 through 16
    0.1458    0.2813    0.1458    0.3125    0.1771    0.1875    0.0938    0.1875
    0.1875    0.1146    0.2292    0.1354    0.0625    0.2708    0.2396    0.0729
    0.0625    0.0521    0.1563    0.0417    0.0938    0.1042    0.2604    0.1042
    0.2083    0.2500    0.1042    0.0729    0.2813    0.1042    0.1042    0.0833
    0.0625    0.1354    0.1875    0.0938    0.1458    0.2292    0.1979    0.1771

i want to add the values of column row wise. i.e i have 16 column in row first, i need the sum of all 16 column of first row, row 2, row 3, row 4 and row 5.

how can i do it ? thanks in advance

1 Comment

Azzi Abdelmalek on 6 Feb 2013

Give a small example that correspond to your problem

M@lik Ali

Tags

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 6 Feb 2013
sum(all_one_matrix(:,:,1), 2)

9 Comments

M@lik Ali on 6 Feb 2013

Yes, but actually in my case i have a small matrix at each index, in the case where you have only value it preserve, already i tested that, but this time i have a complete small matrix at every index of fset. Please suggest me a best solution i am stuck here.

Walter Roberson on 6 Feb 2013

Please give a specific example of data and the command you use, and show the particular output value we should look at and the result you expect and the result you currently get.

M@lik Ali on 6 Feb 2013

ok i am going to put here the matrix i have a matirx fset as below fset(:,:,i), i=1,2,3,4...999

fset(:,:,1) =

Columns 1 through 8
    0.1458    0.0937    0.0313    0.0833    0.1354    0.0521    0.0104    0.0625
    0.2188    0.1563    0.1771    0.2604    0.0833    0.0521    0.1979    0.2708
    0.0417    0.0938    0.0417    0.0313    0.0729    0.0938    0.0313    0.0625
    0.0625    0.0208    0.0208    0.0208    0.0417    0.0625    0.0625         0
    0.2396    0.2083    0.3125    0.2500    0.3021    0.2917    0.3125    0.2708
Columns 9 through 16
    0.0729    0.0521    0.0625    0.1354    0.1771    0.2292    0.2083    0.2708
    0.0521    0.0104    0.0625    0.0521    0.0313    0.1771    0.1042    0.0417
    0.0833    0.1146    0.0208    0.1146    0.0208    0.0104    0.0521    0.0625
    0.0313    0.0833    0.0208    0.0729    0.1250    0.0208    0.0312    0.0625
    0.1875    0.1771    0.1146    0.2188    0.1146    0.0104    0.0104         0

fset(:,:,2) =

Columns 1 through 8
    0.1042    0.0833    0.0729    0.0938    0.0417    0.1563    0.4167    0.1354
    0.0417    0.0625    0.2083         0    0.0729    0.1146    0.1042    0.0313
    0.0417    0.0104    0.0625    0.0729    0.0417    0.0104    0.0417    0.0208
    0.0313    0.0104         0    0.0521    0.1250    0.0521    0.0104    0.0521
    0.0729    0.1771    0.2500    0.1458    0.0208    0.1771    0.3854    0.1562
Columns 9 through 16
    0.0417    0.0313         0    0.1146    0.2813    0.2396    0.1667    0.2813
         0    0.0104    0.0104    0.0625    0.1563    0.1250    0.0417    0.1146
    0.0208    0.0208    0.0104    0.0625    0.0208    0.0313    0.0833    0.0104
    0.1563    0.0208         0    0.0104    0.0521    0.0417    0.0625    0.1354
    0.1250    0.0104    0.0313    0.0521    0.1146    0.0938    0.3021    0.0313

sorted_fset(:,:,1) =

Columns 1 through 8
         0    0.0312    0.0104    0.0313    0.0208    0.0521    0.0104    0.0104
    0.0417    0.0208    0.0417         0    0.0208         0    0.0208    0.0313
    0.0104    0.0104    0.0208    0.0208         0         0         0    0.0104
         0         0         0    0.0208    0.0313    0.0104    0.0104         0
    0.0208    0.0208    0.0417    0.0208    0.0208    0.0208    0.0104    0.0104
Columns 9 through 16
    0.0104    0.0104         0         0    0.1667    0.1771    0.0417    0.0833
         0    0.0104         0         0    0.0313    0.1250    0.0208    0.0104
    0.0208    0.0104    0.0104    0.0104         0    0.0104    0.0521    0.0104
    0.0313    0.0208         0    0.0104    0.0313    0.0104    0.0104    0.0208
    0.0208    0.0104    0.0208    0.0313    0.0417    0.0104         0         0

sorted_fset(:,:,2) =

Columns 1 through 8
    0.1042    0.0833    0.0313    0.0833    0.0417    0.0833    0.0104    0.0208
    0.0417    0.0625    0.0521    0.0208    0.0208    0.0313    0.0417    0.0417
    0.0104    0.0313    0.0417    0.0313    0.0417    0.0104         0    0.0208
    0.0104    0.0104    0.0104    0.0313    0.0417    0.0104    0.0104         0
    0.0521    0.0417    0.0417    0.0625    0.0208    0.0625    0.0521    0.0625
Columns 9 through 16
    0.0208    0.0313    0.0417    0.0521    0.1771    0.2292    0.0521    0.1979
    0.0417    0.0104    0.0104    0.0104    0.0313    0.1250    0.0417    0.0417
    0.0313    0.0208    0.0104    0.0625    0.0104    0.0208    0.0729    0.0313
    0.0625    0.0521    0.0104    0.0104    0.0521    0.0104    0.0312    0.0625
    0.0833    0.0417    0.0313    0.0417    0.0417    0.0417         0    0.0208

sorted_fset(:,:,3) =

Columns 1 through 8
    0.1458    0.0937    0.0313    0.0833    0.0521    0.1042    0.0104    0.0625
    0.0833    0.1146    0.0833    0.0312    0.0729    0.0521    0.1042    0.0521
    0.0313    0.0417    0.0417    0.0313    0.0521    0.0208    0.0313    0.0625
    0.0313    0.0208    0.0208    0.0521    0.0625    0.0104    0.0313    0.0208
    0.0729    0.0729    0.1875    0.1354    0.0729    0.1771    0.1979    0.1250
Columns 9 through 16
    0.0417    0.0313    0.0625    0.1146    0.1875    0.2396    0.1458    0.2708
    0.0521    0.0833    0.0313    0.0521    0.0417    0.1250    0.0625    0.0417
    0.0417    0.0625    0.0208    0.0625    0.0208    0.0208    0.0833    0.0417
    0.1458    0.0729    0.0104    0.0208    0.0521    0.0208    0.0312    0.0729
    0.1250    0.0521    0.0521    0.0521    0.1146    0.0625    0.0104    0.0208

and this is the index index(:,:,1) =

Columns 1 through 13
     5     5     4     4     4     1     1     6     4     4     2     4     3
     2     6     3     2     5     5     5     2     2     1     4     4     1
     4     2     5     6     5     4     4     4     2     5     2     4     5
     6     5     2     1     5     4     2     1     1     2     2     2     6
     5     4     4     5     2     6     4     5     5     2     6     6     5
Columns 14 through 16
     3     5     3
     2     5     3
     1     1     2
     4     4     4
     1     3     1

index(:,:,2) =

Columns 1 through 13
     2     2     1     1     2     3     5     4     3     2     6     6     1
     5     2     5     5     6     6     4     4     3     2     2     6     6
     6     6     1     4     2     2     5     2     6     2     5     2     4
     4     2     4     5     1     5     6     6     5     5     5     6     2
     6     6     5     6     4     5     5     4     6     6     2     4     6
Columns 14 through 16
     1     3     6
     3     2     1
     4     5     5
     5     1     1
     4     5     3

index(:,:,3) =

Columns 1 through 13
     1     1     5     5     5     4     6     1     2     5     1     2     5
     4     5     4     4     2     1     6     6     1     3     5     1     3
     5     3     4     1     6     3     1     1     3     3     3     5     1
     2     1     1     2     4     6     3     5     4     6     3     4     4
     2     5     6     4     5     2     6     6     2     5     5     2     2
Columns 14 through 16
     2     6     1
     6     3     6
     6     2     3
     1     6     5
     3     1     4

now i want after sorting the index as original index, but what is going to happen, if the fset(:,:,3) has all zeros value and fset(:,:,2) has only fset(1,1,2)=0 now when i access index(1,1,1), it should return me 3, because the fset(:,:,3) is the lowest matrix, but it give me the 2 because fset(1,1,2)=0, while scanning it read this first and return the value 2. i want the correct index as in original

thanks, if still any confusion please comment again

Walter Roberson

Contact us