How-To's
numbers
Expressions
Try to re-write the expression somehow.
A safe way to find roots of a quadratic:
x1 = ( −b−sign(b)√(b2−4ac) ) / 2a ,
x2 = c / ax1
e.g. ax2 + bx + c = c + x*(b+a*x) ,
e.g. ax3 + bx2 + cx + d = d + x*(c+x*(b+a*x)) , etc
Matlab
Caution:
sum, min, max on a matrix A operate on each column of A.
Use A(:) = col array of columns of A.
M = input('enter M: ')
N = input('enter N: ')
A = 10*rand(M,N);
%----using array ops: on A(:), else they apply on each column
arraySum = sum( A(:) ); fprintf(' arraySum= %f \n', arraySum )
[arrayMin, iarrayMin] = min( A(:) )
fprintf(' arrayMin= %f at i: %d \n', arrayMin, iarrayMin )
[arrayMax, iarrayMax] = max( A(:) )
fprintf(' arrayMax= %f at i: %d \n', arrayMax, iarrayMax )
%----using loops:
Sum = 0 ; Min = Inf ; Max = -Inf ;
for i = 1:M % for each row i
for j = 1:N % for each col j
aij = A(i,j); %save to avoid multiple lookups
Sum = Sum + aij;
if( aij <= Min )
Min = aij; iMin = i; jMin = j;
end %if
if( aij >= Max )
Max = aij; iMax = i; jMax = j;
end %if
end %j
end %i
fprintf(' Sum= %f \n', Sum)
fprintf(' Min= %f at i,j: %d %d \n', Min, iMin, jMin )
fprintf(' Max= %f at i,j: %d %d \n', Max, iMax, jMax )
input linspace rand randi
plot fopen,fclose
fprintf fscanf fgetl feof
zeros ones length size
sort sum min max norm