the largest positive element (m∞ ) f = [2 , -5,5,4 in matlab

To find the largest positive element from a given matrix/vector, we can use the following code snippet:

main.m
f = [2, -5, 5, 4];
m_inf = max(f(f > 0));
42 chars
3 lines

In this code, we first define the vector f as [2, -5, 5, 4]. We then use the max() function with a logical index of f > 0 to find the maximum of only the positive elements in f. The final result is stored in the variable m_inf.

Note that if there are no positive elements in f, m_inf will be empty, so you may want to add a check for this condition.

related categories

gistlibby LogSnag