Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Adding rows in a matrix based on certain criteria
Replies: 2   Last Post: Feb 7, 2013 9:58 AM

 Search Thread: Advanced Search

 Messages: [ Previous | Next ]
 Barry Shaw Posts: 32 Registered: 2/16/12
Re: Adding rows in a matrix based on certain criteria
Posted: Feb 7, 2013 9:58 AM
 Plain Text Reply

It would be helpful if you posted what you have attempted and any errors. I'm not sure if I've grasped what you're trying to. Does this help?

m = 5;
matrix = [[1 20 3 4 1]' [1 1 10 2 1]']; % for example

for i = 1 : m
if matrix(i,1) == 1 & matrix(i,2) == 1
matrix(i,3) = matrix(i,1) + matrix(i,2);
else
matrix(i,3) =nan(1); % What do you want to do if cols 1 and 2 don't equal 1?
end
end
matrix

"Kumar" wrote in message <kf07gs\$f2u\$1@newscl01ah.mathworks.com>...
> I'm new to Matlab programmin and this may look like a simple question. I have to add rows in matrix based on certain criteria. For example, if the values in column 1 is 1 and values in cloumn 2 is 1, then i have to add elements in colum 3 of all such rows. In the matrix, elemets in column 1 varies from 1-20 and elements in colum 2 varies from 1-10. the size of the matrix is m by 3.

Date Subject Author
2/7/13 dpb
2/7/13 Barry Shaw

© The Math Forum at NCTM 1994-2018. All Rights Reserved.