absolute values matlab in matlab

In Matlab, you can use abs function to calculate absolute values of a matrix/vector.

For example, consider the following matrix:

main.m
A = [-2 4 -6; 7 -8 9];
23 chars
2 lines

To calculate the absolute values of matrix A, you can do the following:

main.m
abs_A = abs(A);
16 chars
2 lines

This will give you the following result:

main.m
abs_A =
     2     4     6
     7     8     9
46 chars
4 lines

Similarly, to calculate the absolute value of a single number in Matlab, you can use the abs function as follows:

main.m
x = -3;
abs_x = abs(x);
24 chars
3 lines

The output will be:

main.m
abs_x =
     3
15 chars
3 lines

gistlibby LogSnag