Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

New with MathCad

Status
Not open for further replies.

autiger

Structural
Jul 13, 2001
5
US
Guys, I could use a little help. I am trying to learn MathCad and have come up on something I need a little help with. I have a matrix as shown below.

A = 1 1
0 0
0 0

I need to coun the number of "1"s and the number of "0"s. I set variables named NumFree and NumRest to zero. If A i,j is one, then I want to add 1 to NumRest. If A i,j is zero, I want to add 1 to NumFree.

Any help would be greatly appreciated.

 
Replies continue below

Recommended for you

Possibly the simplest method is to use the summation and vectorize operators. Try

NumFree:=sum(vectorize(A=0))
NumRest:=rows(A)*cols(A)-NumFree

where
sum represents the vector sum operator ([ctrl]4)
vectorize(A=0) represents the vectorize operator ([ctrl]-) applied to the expression A=0, where = is the boolean equals ([ctrl]=)
 
ex - you are assuming that 0 and 1 are the only valid elements. The OP does not explicitly state this.

Cheers

Greg Locock

SIG:please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
For instance

0 1
0 2

Has too 0s, and one 1

your method would give the wrong answer

Cheers

Greg Locock

SIG:please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Greg,

I'm guessing from the variable names that NumFree is meant to count something that is available, in the sense of 'not taken' (or doesn't cost anything!) and zero is the measure of that state. NumRest I took to be 'The rest of them' in which case, what I propose should work - count the zeros (NumFree) and take that number away from the total number of elements to get the Rest.

However, you're quite right; without a full quantification it is simply guess work.
 
Guys, thanks for the help. The only numbers in the matrix will be 1's and 0's.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top