Function If If Elseif Elseif Elseif Else End Else End
The document contains code for multiple functions:
1. The valid_date function checks if a date defined by year, month and day inputs is valid.
2. The under_age function checks if an age is below a limit, defaulting to 21 if no limit is provided.
3. The sparse2matrix function converts a sparse matrix cell vector representation to a full matrix.
4. Several other functions perform operations like finding saddle points in a matrix, the next prime number, the maximum sum of subsequences from a vector, and more.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
59 views13 pages
Function If If Elseif Elseif Elseif Else End Else End
The document contains code for multiple functions:
1. The valid_date function checks if a date defined by year, month and day inputs is valid.
2. The under_age function checks if an age is below a limit, defaulting to 21 if no limit is provided.
3. The sparse2matrix function converts a sparse matrix cell vector representation to a full matrix.
4. Several other functions perform operations like finding saddle points in a matrix, the next prime number, the maximum sum of subsequences from a vector, and more.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13
function valid = valid_date(year,month,day)
if sum(rem([year,month,day],1))==0 && sum([year,month,day]>0)==3
if ismember(month,[1,3,5,7,8,10,12]) && day<32 valid=true; elseif ismember(month,[4,6,9,11]) && day<31 valid=true; elseif month==2 && ismember(sum(rem(year,[4,100,400])==0),[1,3]) && day<30 valid=true; elseif month==2 && ismember(sum(rem(year,[4,100,400])==0),[0,2]) && day<29 valid=true; else valid=false; end else valid=false; end function too_young = under_age(age,limit) if nargin==1 limit=21; end if age<limit too_young = true; else too_young = false; end function matrix = sparse2matrix(Cellvec) aux=length(Cellvec); matrix=Cellvec{2}*ones(Cellvec{1}(1),Cellvec{1}(2)); for i = 3:aux matrix(Cellvec{i}(1),Cellvec{i}(2))=Cellvec{i}(3); end function indices = saddle(M) [n,m]=size(M); indices=zeros(0,2); for i=1:n for j=1:m if M(i,j)==min(M(:,j)) && M(i,j)==max(M(i,:)) indices(end+1,:)=[i,j]; end end end function k= next_prime(n) k=n+1; while isprime(k)==0 k=k+1; end function [summa,index] = max_sum(V,n) if length(V)<n summa=0; index=-1; else for i=1:length(V)-n+1 aux(i)=sum(V(i:i+n-1)); end summa=max(aux); index=find(summa==aux, 1 ); end function summa = halfsum(A) [n,m]=size(A); summa=0; for i = 1:n for j=i:m summa=summa+A(i,j); end end function distance = get_distance(state1,state2) [~, ~, raw] = xlsread('Distances.xlsx'); index1=find_index(raw,state1); index2=find_index(raw,state2); if index1~=-1 && index2~=-1 distance=raw{index1,index2}; else distance=-1; end function index = find_index(matrixcell,value) index=-1; for i=2:length(matrixcell) if strcmp(matrixcell{1,i},value) index=i; end end function numfreeze = freezing(V) numfreeze=sum(V<32); end function admit = eligible(v,q) if mean([v q])>=92 && v>88 && q>88 admit = true; else admit = false; end function output = echo_gen(in,fs,delay,gain) samples = round(fs * delay); ds = floor(samples); signal = zeros(length(in) + ds, 1); signal(1:length(in)) = in; echo_signal = zeros(length(in) + ds, 1); echo_signal(ds + (1:length(in*gain))) = in * gain; output = signal + echo_signal; p = max(abs(output)); if p > 1 output = output ./ p; end end function charnum = char_counter(file_name, character) file = fopen(file_name, 'rt'); if file < 0 || ~ischar(character) charnum = -1; return; end charnum = 0; line = fgets(file); while ischar(line) charnum = charnum + frequency(line, character); line = fgets(file); end end function count = frequency(line, character) count = sum(line == character); end function coded = caesar(v,n) aux=double(v)+rem(n,95); coded=char(aux+95*((aux<32)-(aux>126))); end