Examples of Matlab 24-7-18
Examples of Matlab 24-7-18
Examples of Matlab 24-7-18
for i = 1:1:row
for j = 1:1:col
currNo = matA(i,j);
st1 = strcat('The value being tested is: ', num2str(currNo),'.');
disp(st1)
if (currNo > 3)
disp('The current value is larger than 3.')
else
disp('The current value is less or equal than 3.')
end
end
end
Don’t know what the shit this does
x = 1:10;
y = x*t^2; % just an example
t = 1:10;
m = [x' zeros(numel(x),numel(t))]; % preallocate and fill in first column
with 'x'
for r = 1:numel(x) % row counter
for c = 1:numel(t) % column counter
m(r,c+1) = y(x(r),t(c)); % note the 'c+1' column subscript for the
output matrix
end
end
Fibonacci
n=10;
f = zeros(n,1);
f(1) = 0;
f(2) = 1;
for k = 3:n
f(k) = f(k-1) + f(k-2);
end
disp(f')
Matrix loop
% 1 2 3 4 5
% 2 4 6 8 10
% 3 6 9 12 15
A=zeros(3,5);
if n==1
A(n,m+1)=A(n,m)+1;
elseif n==2
A(n,m+1)=A(n,m)+2;
else
A(n,m+1)=A(n,m)+3;
end
end
end
disp(A)
For loop
%For loops can be nested as desired:
for n=1:3
for m=3:-1:1
A(n,m)=n^2+m^2;
end
end
disp(A)
Plot
function [v]=plotit(xmin,xmax,npoints);
% plot a graph of a function of a single variable f(x)
% from xmin to xmax, with npoints data points
for n=1:npoints
x(n) = xmin + (xmax-xmin)*(n-1)/(npoints-1);
v(n)=cos(x(n));
end
figure;
plot(x,v);
end
Prime
clc
x=input('give any natural number')
if x==1;
p={'not prime'};
elseif x==2;
p={'prime'};
else
for i=3:x
if rem (x,(i-1))==0 ;
p={'not prime'};
break
else
p={'prime'};
end
end
end
p
Series sum
n=input ('Enter upto how many terms you want to see & sum: ');
num=0;sum=0;num1=[];
for x=1:n
num = num+5;