Use of MATLAB in mathematicsMATLAB is a high-level computing language for programming, technical data analysis, and visualization. It incorporates features for algorithm development, simulation, modeling, and prototyping, as well as functions for data exploration, analysis, and visualization, which ...
clear all, clc
D1=15; % The First Digits or number
D2=10; % The Second Digits (Which we will perform the Two's complement on)
% The addtional 1's for second complement
[c1,carry, num]=TwosComplement(D1,D2);
disp(['The summation of dec1+dec2 in binary format num:= ', num2str(dec2bin(num,8))])
disp(['The summation of dec1+dec2 in decimal format num:= ', num2str(num)])
function [carry1,carry, num]=TwosComplement(D1,D2)
% Carry1: Is the carry out from the result of the Tws compleent of
% D2 with 1
% carry : Is the carry results from the summation of D1+D2
% num : Is the final results in decimal format
if (D2<0) % Checl if the input Digits D2 is negative
% We need to perform the Two's complement by
% Doing first compement and adding 1
bin2=(reshape(dec2bin(abs(D2),8),8, [])');
for ii=numel(bin2):-1:1
% This code for the first complements
if (bin2(ii)=='1')
bin2(ii)='0';
else
bin2(ii)='1';
end
end
dec2=bin2dec(bin2);
% This code perform the second complement
% We add D2+1 if D2 is negative number
[carry1,D2]=addition(1,dec2);
else
carry1=0;
end
% Perform the Addition of the Two numbers
% Using addition function that accept two digit numbers
[carry,decnum]=addition(D1,D2)
% Convert the obtained results (in binary format into decimal format)
num=mod(bin2dec(decnum),255); % Display a decimal number
function [carry,decsum]=addition(dec1,dec2)
% decsum : The output of the sum of two binary numbers bin1
% (which is the binary representation of dec1, and bin2 for dec2)
% carry : The Carry in once there is summation of '1'+'1' at the last
% bits of the two number dec1 and dec2
% Addition of two dec numbers
% dec1, and dec2
% The First Digit has to be positive dec1
% The Second one can be +dec2 or -dec2
bin1=(reshape(dec2bin(dec1,8),8, [])');
bin2=(reshape(dec2bin(abs(dec2),8),8, [])');
k=0;
decsum =char(zeros(1,length(bin1)));
carry=0;
% The for loop perform the addition of two binary numbers dec1 and dec2 (are converted intwo binary format)
for row=length(bin1):-1:1
if (((bin1(row)=='0') && (bin2(row)=='1')) || ((bin1(row)=='1') && (bin2(row)=='0')))
decsum(row)='1';
elseif ((bin1(row)=='1') && (bin2(row)=='1'))
decsum(row)='1';
carry='1';
% Check where is the carry bit is
k=row;
else
decsum(row)='0';
end
end
if (k<=length(bin1))
decsum(9-k)='1'; % set the last row if k is less then 8
carry='0'
else
carry='1';
end
end
end
function [out] = betterDataAnalysis(Arr, Flower)
%BETTERDATAANALYSIS Summary of this function goes here
% Detailed explanation goes here
fidx = find(strcmp(Arr(1, :), 'Flower Name'));
hidx = find(strcmp(Arr(1, :), 'Height'));
widx = find(strcmp(Arr(1, :), 'Width'));
Arr = Arr(:, [fidx, hidx, widx]);
Arr(2:end, 4) = num2cell(round(cell2mat(Arr(2:end, 2)).*cell2mat(Arr(2:end, 3)), 2));
Arr(1, 4) = {'Volume'};
nremove = 4;
[~, idx] = maxk(cell2mat(Arr(2:end, 4)), size(Arr, 1)-nremove-1);
idx = [1; idx+1];
Arr = Arr(idx, :);
idx = logical([0; strcmpi(Arr(2:end, 1), Flower)]);
Arr = Arr(~idx, :);
[~, idx] = sort(lower(Arr(2:end, 1)));
idx = [1; idx+1];
Arr = Arr(idx, :);
out = Arr;
out(:, fidx) = Arr(:,1);
out(:, hidx) = Arr(:,2);
out(:, widx) = Arr(:,3);
end
function [pizza, feedback] = boneless(pizza)
%BONELESS Summary of this function goes here
% Detailed explanation goes here
loc = strcmpi('bones', pizza);
pizza(loc) = {'pepperoni'};
num = sum(loc);
if (num == 0)
feedback = 'Bruh that was BONELESS!';
elseif(num == 1)
feedback = 'Well there''s a bone in my pizza but maybe it was an accident.';
else
feedback = 'Absolutely unacceptable, I found ' + string(num) + ' bones in my pizza!';
end
end
function [atoms] = elementCounter(data)
%ELEMENTCOUNTER Summary of this function goes here
% Detailed explanation goes here
atoms = {};
for atom = data
[elems, tok, ~] = regexp(atom{1},'([A-Z][a-z]*)(\d*)','match', 'tokens', 'tokenExtents');
for i = 1:size(elems, 2)
elem = tok{1, i}{1};
num = str2num(tok{1, i}{2});
if (isempty(num))
num = 1;
end
if(size(atoms, 1) > 0)
idx = strcmp(atoms(1, :), elem);
if(sum(idx) == 0)
atoms = [atoms, {elem; num}];
else
atoms{2, idx} = atoms{2, idx} + num;
end
else
atoms = {elem; num};
end
end
end
atoms = sortrows(atoms', 1)';
end
% * input
% * solve
% * fclose all
% * close
% * load
% * figure
% * imshow
% * image
% * global
%
%% Files to Submit
%
%
% - hw07.m
% - boneless.m
% - betterDataAnalysis.m
% - koolAidMan.m
% - rapLyrics.m
% - elementCounter.m
%
%% Test Cases
%
%% boneless
load('boneless.mat');
[pizza] = boneless(pizza1);
[pizza_soln] = boneless_soln(pizza1);
check1 = isequal(pizza, pizza_soln);
[pizza] = boneless(pizza2);
[pizza_soln] = boneless_soln(pizza2);
check2 = isequal(pizza, pizza_soln);
[pizza] = boneless(pizza3);
[pizza_soln] = boneless_soln(pizza3);
check3 = isequal(pizza, pizza_soln);
%% betterDataAnalysis
load('betterDataAnalysis.mat');
[out] = betterDataAnalysis(table1, flower1);
[out_soln] = betterDataAnalysis_soln(table1, flower1);
check1 = isequal(out, out_soln);
[out] = betterDataAnalysis(table2, flower2);
[out_soln] = betterDataAnalysis_soln(table2, flower2);
check2 = isequal(out, out_soln);
[out] = betterDataAnalysis(table3, flower3);
[out_soln] = betterDataAnalysis_soln(table3, flower3);
check3 = isequal(out, out_soln);
%% koolAidMan
load('koolAidMan.mat');
[location] = koolAidMan(building1);
[location1] = koolAidMan_soln(building1);
check1 = isequal(location, location1);
[location] = koolAidMan(building2);
[location2] = koolAidMan_soln(building2);
check2 = isequal(location, location2);
[location] = koolAidMan(building3);
[location3] = koolAidMan_soln(building3);
check3 = isequal(location, location3);
%% rapLyrics
load('rapLyrics.mat');
[ca] = rapLyrics(lyric1);
[ca_sol] = rapLyrics_soln(lyric1);
check1 = isequal(ca, ca_sol);
[ca] = rapLyrics(lyric2);
[ca_sol] = rapLyrics_soln(lyric2);
check2 = isequal(ca, ca_sol);
[ca] = rapLyrics(lyric3);
[ca_sol] = rapLyrics_soln(lyric3);
check3 = isequal(ca, ca_sol);
%% elementCounter
load('elementCounter.mat');
[out] = elementCounter(elements1);
[out_soln] = elementCounter_soln(elements1);
check1 = isequal(out, out_soln);
[out] = elementCounter(elements2);
[out_soln] = elementCounter_soln(elements2);
check2 = isequal(out, out_soln);
[out] = elementCounter(elements3);
[out_soln] = elementCounter_soln(elements3);
check3 = isequal(out, out_soln);
function [out] = koolAidMan(building)
%KOOLAIDMAN Summary of this function goes here
% Detailed explanation goes here
for r = 1:size(building, 1)
for c = 1:size(building, 2)
house = building(r,c);
counter = 0;
while(iscell(house))
house = house{1};
counter = counter+1;
end
if(~isempty(house))
break
end
end
if(~isempty(house))
break
end
end
out = 'Oh yeah! The Kool-Aid man burst through '+string(counter)+ ...
' walls at row '+string(r)+', column '+string(c)+'.';
end
function [ca] = rapLyrics(str)
%RAPLYRICS Summary of this function goes here
% Detailed explanation goes here
str_list = strsplit(lower(str));
idx_list = [];
index = 1;
counter = 1;
for each_str = str_list
idx_list(index, 1) = counter;
counter = counter + length(each_str{1}) + 1;
index = index+1;
end
[str_list, sort_idx] = sort(str_list);
prev_str = str_list{1};
ca = {prev_str; idx_list(sort_idx(1)); 1};
for i = 2:length(str_list)
str = str_list{i};
if(~strcmp(str, prev_str))
ca = [ca, {str; idx_list(sort_idx(i)); 1}];
prev_str = str;
else
ca{3, end} = ca{3, end} + 1;
end
end
ca = sortrows(ca', 3, 'descend')';
end