Adding Column values of a matrix

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

Give a small example that correspond to your problem

Sign in to comment.

Answers (1)

sum(all_one_matrix(:,:,1), 2)

9 Comments

Nice it is working fine, can you help me for this. I have a matrix like below
fset(:,:,1) = rand(5,16); fset(:,:,2) = rand(5,16); fset(:,:,3) = zeros(5,16);
i sorting as [sorted_fset iundex]= sort(fset,3); My problem is i have 999 small matrix in a big matrix like this means fset(:,:,i),i=1,2,3,4....999 I represent the each image actually, now when i sort the matrix it sort for me but i cannot recognize the index of each image. Like in above case fset(:,:,3) will be fset(:,:,1) but actually it was 3, so how can i recognize the image index after sorting in this case. if any confusion please comment
You said "Like in above case fset(:,:,3) will be fset(:,:,1) but actually it was 3" but you are referring to the same array in both cases. Perhaps you meant to refer to iundex or sorted_fset in one of those two variables?
The first output index would be 3 because you set plane 3 to all zeros and rand() is always greater than 0 so plane 3 is going to sort earlier than plane 1 or 2. The 3 in the first position indicates that the 3rd entity was what sorted first.
yes you are right, the set(:,:,3) will be sorted_fset (:,:,1) now i want to know the actual index of the sorted_fset (:,:,1), which is fset(:,:,3), so how i can do it.
Look at iundex(:,:,1)
Already try this, but this return me the wrong results. actually during sorting it pick the first lowest value. means if sorted_fset (:,:,1) has 0 at its first index, and sorted_fset (:,:,2) has all zero values then index(:,:,1) will be 1, but it should be 2 as the sorted_fset (:,:,2) has all zeros
[B,IX] = sort(A,...) [...]
If A has repeated elements of equal value, the returned indices preserve the original ordering.
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.
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.
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

Sign in to comment.

Categories

Tags

Asked:

on 6 Feb 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!