f



Conditional Sum Matrix

Hi guys,

I have a matrix of 2 columns and 85 rows. The second column contains these numbers: 1, 2, 3, 4, 5 and 6. I want to sum elements of the first column with the condition that the equivalent element from the second column = 1 for example. How can I do that without using a loop?

Thanks a lot.

Best,
Hamza
0
Hamza
12/20/2016 4:57:03 PM
comp.soft-sys.matlab 211264 articles. 26 followers. lunamoonmoon (257) is leader. Post Follow

2 Replies
689 Views

Similar Articles

[PageSpeed] 9

"Hamza Chiheb" wrote in message <o3bnsv$b0m$1@newscl01ah.mathworks.com>...
> Hi guys,
> 
> I have a matrix of 2 columns and 85 rows. The second column contains these numbers: 1, 2, 3, 4, 5 and 6. I want to sum elements of the first column with the condition that the equivalent element from the second column = 1 for example. How can I do that without using a loop?
> 
> Thanks a lot.
> 
> Best,
> Hamza

x = your 85x2 matrix
result = sum(x(x(:,2)==1,1));

James Tursa
0
James
12/20/2016 6:17:03 PM
Thanks a lot James!
0
Hamza
12/20/2016 6:32:03 PM
Reply: