how to sum column of matrix?

2 次查看(过去 30 天)
hello everyone, I want to sum the percentage of the first three columns from the A matrix acoording the matrix T, there is an exemple of matrix :
A=[1,3,15,598,58,15,25,4,9,10;
4,4,25,1,1,9,47,121,44,12;
10,11,47,3,5,7,98,15,55,888;
4,8,1,23,58,444,17,9,8,5;
7,25,14,89,547,3,10,11,15,47;
1,15,24,,1,5,947,5,48,7,12;
47,8,5,1,2,69,78,555,47,11 ];
T=[1;4;11;1;100;54;5]
I want to compare the first column of A with the matrix T and calculate the percentage that this column contains the same value than the matrix T. the next step is to compare the second column of A with T and add the percentage that this column contains some values missing compared to the first column. the final step is similar than later but using the third column.
for more explanation, here is a small example
a=[1,5,8,9;
4,4,7,1;
9,5,48,175;
5,48,4,2,1 ====>first-clm=[1,4,9,5,2] than pourcentage =40%
2,58,4,1,7] ====>second-clm=[5,4,5,48,58] than pourcentage =20% =========> the final pourcentage is 80%
t=[1,4,5,4,11] ====>third-clm=[8,7,48,4,4] than pourcentage =20%
please help me, How can do this in matlab?

采纳的回答

Adam
Adam 2016-5-25
idx = bsxfun( @eq, a, t' );
percent = 100 * sum( any( idx, 2 ) ) / numel( t );
would work for your second example. In the first example your T is a column vector rather than a row vector so you would want to remove the transpose from the first line - i.e.
idx = bsxfun( @eq, A, T );
should work there.
  4 个评论
aya ben mabrouk
aya ben mabrouk 2016-5-25
I tryed agin and I obtain the same error
A=[1,3,15,598,58,15,25,4,9,10;
4,4,25,1,1,9,47,121,44,12;
10,11,47,3,5,7,98,15,55,888;
4,8,1,23,58,444,17,9,8,5;
7,25,14,89,547,3,10,11,15,47;
1,15,24,,1,5,947,5,48,7,12;
47,8,5,1,2,69,78,555,47,11 ];
T=[1;4;11;1;100;54;5]
idx = bsxfun( @eq, A, T );
percent = 100 * sum( any( idx, 2 ) ) / numel( T );
Error: File: test1.m Line: 9 Column: 16
Expression or statement is incorrect--possibly unbalanced (, {, or [.
aya ben mabrouk
aya ben mabrouk 2016-5-25
so good, I deleted the comma and it work now. but I want to have a matrix that contain the percentage of every column like this in the later example :
percent=[percent of thirst column, percent of thirst+second column, the total percent ];
percent=[40,60,80]

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by