Sum every i-th column in matrix seperately

Hi all,
I have randomly generated matrix and I want to go through all columns and to sum every i-th column separately. I don't want to sum all the columns but to sum depending on the counter in for loop for example:
[a,b] = size(C);
for i = 1:b
S = sum(C(:,i))
S = 0 %but this doesn't work, result is sum of elements of all columns

 採用された回答

Zikobrelli
Zikobrelli 2014 年 6 月 12 日

0 投票

Try sum(A(:,[1 4]))
where A is your random matrix.The line above will give you the sums of column 1 and 4.
ex: A=spiral(4)
sum(A(:,[1 4]))
ans = 34 46

8 件のコメント

Anya
Anya 2014 年 6 月 12 日
Please look at my code above and example. Your answer is not clear to me
Zikobrelli
Zikobrelli 2014 年 6 月 12 日
Sum=[]
for i=1:size(C,2)
if (sum(C(:,i))~=0 & sum(C(:,i))~=4)
Sum=[Sum sum(C(:,i))]
end
end
Sum is the answer. As i wanted, i does not include the sums of columns whose values are either all 1 or 0.
for your example, Sum=[ 1 3 2 1 1 2 3 2] Hope this helps
Anya
Anya 2014 年 6 月 12 日
I tried to put your code into mine but it's not working as I expected. Sorry I'm new with Matlab but with the code above columns with all ones or zeros are not skipped
Zikobrelli
Zikobrelli 2014 年 6 月 12 日
C=[1 1 1 1 0 1 1 1 1 0 0 1 1 0 0 0 1 1 1 0 0 1 0 1 0 0 0 1 0 0 0 1 1 0 1 0 0 0 0 0];
v=[];
[a,b] = size(C);
for i = 1:b
if (sum(C(:,i)) == 0) (sum(C(:,i)) == a)
'I-th column is zero or ones , move on'
else v=[v sum(C(:,i))]
end
end
This isnt working for you?
Zikobrelli
Zikobrelli 2014 年 6 月 12 日
enter your matrix instead of C. i coied it as a vector, sorry
Anya
Anya 2014 年 6 月 12 日
No, the code is not working, it never enters the if not print 'I-th column is zero or ones , move on'
C = [randi(2,4,10)-1; 1:10]
for i = 1:b
if (sum(C(:,i)) == 0) (sum(C(:,i)) == a)
'I-th column is zero or ones , move on'
else v=[v sum(C(:,i))];
end
end
Zikobrelli
Zikobrelli 2014 年 6 月 12 日
You're using randi(generates random integer numbers) to build your matrix. then you add a last line (1:10) which means that you can NEVER have an all zeros column ,because the last line does not contain 0 Which means that the ONLY way to enter the if statement is to get an all ones column, which indeed does not happen often. The code works, you just need to run the program enough or choose a more suited matrix :)
Anya
Anya 2014 年 6 月 12 日
I can't believe that I missed this last control row . I don't have a lot of experience so I didn't noticed. I change the code now by changing the sum function and it works ! Thanks
sum(C(1:end-1,i))

サインインしてコメントする。

その他の回答 (2 件)

Roger Stafford
Roger Stafford 2014 年 6 月 12 日

1 投票

S = sum(C(:,i:i:end),1);
This results in a row vector, S, consisting of the sum of every i-th column of C, as requested. It is not clear where you want the i-spaced columns to start. This starts at the i-th column. If you want them to start at the first column, change "i:i:end" to "1:i:end".

1 件のコメント

Anya
Anya 2014 年 6 月 12 日
I think that I was not clear enough. I will try to explain on the example matrix C. Using for loop I want to iterate through the columns and count number of non-zero elements. They are written after the last row. If I found the column with all ones or all zeros I want to skip that column and move on.
C =
1 1 1 1 0 1 1 1 1 0
0 1 1 0 0 0 1 1 1 0
0 1 0 1 0 0 0 1 0 0
0 1 1 0 1 0 0 0 0 0
1 4 3 2 1 1 1 3 2 0

サインインしてコメントする。

Jos (10584)
Jos (10584) 2014 年 6 月 12 日

0 投票

sumColsC = sum(C,1)
NotInteresting = sumColsC == 0 | sumColsC == size(C,2)
sumColsC(NotInteresting) = []

3 件のコメント

Anya
Anya 2014 年 6 月 12 日
編集済み: Anya 2014 年 6 月 12 日
I done it on this way but it doesn't work , it never enters in the if statements , where is my mistake or how can I apply your code to mine?
C = [randi(2,4,10)-1; 1:10]
[a,b] = size(C);
for i = 1:b
if (sum(C(:,i)) == 0)
'I-th column is zero , move on'
i=i+1;
end
if (sum(C(:,i)) == a)
'I-th column is ones, move on'
i=i+1;
end
Zikobrelli
Zikobrelli 2014 年 6 月 12 日
you're generating a random matrix.So yes, sometimes, you will not enter the if condition :)
try this
v=[]; C = [randi(2,4,10)-1]
[a,b] = size(C);
for i = 1:b
if (sum(C(:,i)) == 0) || (sum(C(:,i)) == a)
'I-th column is zero or ones , move on'
else v=[v sum(C(:,i))]
end
end
Jos (10584)
Jos (10584) 2014 年 6 月 12 日
When you change the iterator in a for-loop, it will reset at the end
for k=1:10
disp(k) ;
k = 1 ;
disp(k) ;
end
You should be clearer about your goals. What do you mean with "move on"?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2014 年 6 月 12 日

コメント済み:

2014 年 6 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by