Mathcad: Count Elements Divisible by 3 Per Row Vector
Learn how to create a vector in Mathcad counting elements divisible by 3 in each row of matrix M. Use vectorized sum with mod or programming loops for efficient row-wise counts.
In MathCad, how do I create a vector that records the count of elements divisible by 3 in each row of an existing matrix M? Provide a programming module or code example for this task, assuming M was defined previously.
In Mathcad you can get a per-row vector of counts for elements divisible by 3 either by summing a boolean test across each row or by writing a small programming block. A concise, reliable formula is:
countRow(i) := sum( (M[i,j] mod 3 = 0), j, 0, cols(M)-1 ) then counts := countRow(0..rows(M)-1). For those who prefer imperative code, a short nested-for programming region does the same.
Contents
- Quick vectorized solution (boolean + sum)
- Program module — loop-based (imperative)
- Example and expected output
- Notes: indexing, floating-point and performance
- Sources
- Conclusion
Quick vectorized solution (boolean + sum)
This is the simplest, fastest approach in Mathcad: build a boolean test for divisibility by 3, then sum that test across each row because Mathcad treats true as 1 and false as 0.
Core steps (copy into a Mathcad worksheet):
m := rows(M)
n := cols(M)
countRow(i) := sum( ( M[i, j] mod 3 = 0 ), j, 0, n-1 )
counts := countRow(0..m-1)
rows(M)andcols(M)give the matrix dimensions.sum(expression, j, 0, n-1)sums the expression over column indexj.- The boolean expression
(M[i, j] mod 3 = 0)yields 1 for values divisible by 3 and 0 otherwise, so the sum is the count per row.
Why this is recommended: it’s concise, vectorized and generally much faster than explicit nested loops for large matrices. The same boolean + sum trick is discussed in the PTC community examples on counting vector/matrix elements (see the community links in Sources).
Program module — loop-based (imperative)
If you want an explicit programming block (for clarity or because you prefer imperative style), create a Programming Region from Mathcad’s Programming palette and implement nested loops that increment a counter per row.
Readable pseudocode you can paste into a Programming Region (adapt small syntax differences to your Mathcad version as needed):
// Pseudocode for a Mathcad Programming Region m := rows(M) n := cols(M) // Option A: preallocate (preferred) — create a zero vector of length m // (use your Mathcad version's way to make a zero vector) counts := zero_vector(m) // replace with the appropriate constructor if needed for i from 0 to m-1 do c := 0 for j from 0 to n-1 do if ( M[i, j] mod 3 = 0 ) then c := c + 1 end if end for counts[i := c] // assign c to the i-th element of counts (use your version's subscript assignment) end for return counts
Notes on implementing the Programming Region:
- Pre-allocating
counts(a zero vector of lengthm) and then assigningcounts[i := c]inside the outer loop is the cleanest approach. - If you prefer to append items, use your version’s concatenation/augment function (e.g.,
counts := augment(counts, c)) — exact function names vary by Mathcad release. - This imperative module mirrors the logic shown in the PTC forum examples for counting matrix elements by condition.
Example and expected output
Let M be a 3×3 example matrix:
M :=
[ 3 4 6
1 9 12
7 8 10 ]
Using the vectorized solution above:
- Row 0: 3 and 6 are divisible by 3 → count = 2
- Row 1: 9 and 12 are divisible by 3 → count = 2
- Row 2: none are divisible by 3 → count = 0
So counts evaluates to:
counts = [ 2, 2, 0 ]
You can verify the same result with the programming-region implementation.
Notes: indexing, floating-point and performance
- Index base: Mathcad’s default index base is 0 (so ranges
0..n-1are common). If you use 1-based indices in your sheet, adjust the lower/upper bounds in thesumaccordingly (e.g.,j, 1, n). - Non-integers / rounding errors: if matrix entries are floating-point approximations, test divisibility with a tolerance, for example:
isDivisible := abs(mod(M, 3)) < 1e-10- or test
mod(round(M), 3) = 0if values should be integers but have tiny numeric noise. - Negative numbers:
modbehavior can depend on version; usingroundbeforemod(when appropriate) avoids sign/precision surprises. - Performance: the boolean+sum method is vectorized and usually fastest in Mathcad Prime and Classic. Use the imperative nested loops only when you need side-effects or very explicit control.
- Version compatibility: the
sum(expression, index, a, b)pattern and boolean-as-1 behavior are standard in Mathcad; examples and variations appear in the PTC community threads linked below.
For extra examples and community discussion of both the boolean-sum trick and loop-based approaches see the PTC forum posts in Sources.
Sources
- PTC Mathcad Prime Free Download
- Mathcad Prime Features | PTC
- Count elements in a matrix that meet a condition (PTC Community)
- Counting elements in a vector (PTC Community)
- Counting elements in a matrix (PTC Community)
Conclusion
Use the vectorized boolean + sum pattern in Mathcad to build a vector of per-row counts of elements divisible by 3: define a row function with sum((M[i,j] mod 3 = 0), j, 0, cols(M)-1) and evaluate it over 0..rows(M)-1. It’s concise, fast and easy to adapt; if you need a step-by-step routine, the short nested-for programming module does the same job.