How is Matlab useful in mechanical engineering?Matlab is short for MATrixLABoratory, a high-level programming language that allows users to do programming as well as simulations and visualization in a single platform. It makes statistical computations, data analysis, and visualization tasks much eas...
function str = chaosChallenge(Str1, Str2, rules);
M=['Fire ',' Wind ',' Lightning ',' Earth ',' Water '];
Rul=['W ','L ','T'];
M_C=regexp(M,'\s+','split');
R_C=regexp(Rul,'\s+','split');
M_1=strcmp(M_C(1:numel(M_C)),Str1);
M_2=strcmp(M_C(1:numel(M_C)),Str2);
Rules1=rules(M_1,1);
Rules2=rules(M_2,1);
Moves1=rules(1,M_1);
Moves2=rules(1,M_2);
S_D=[Rules1 Rules2];
STR=[Str1,' ',Str2];
STR=cellstr(regexp(STR,'\s+','split'));
MOVES=cellstr(regexp([Moves1,' ',Moves2],'\s+','split'));
if(strfind(S_D,'W')~=0)
m=strfind(S_D,'W');
str=['str-> ''Yay, I won with ', vertcat(cell2mat(STR(m))),'!'''];
disp(str)
elseif(strfind(S_D,'L')~=0)
m=strfind(S_D,'L');
str=['str->''Oh no, my opponent beat me with, ' vertcat(cell2mat(MOVES(m))),'!'''];
disp(str)
elseif(strfind(S_D,'T')~=0)
m=strfind(S_D,'T');
str=['str->''Who knew that', vertcat(cell2mat(STR(m))), 'and' vertcat(cell2mat(MOVES(m))),' would tie?'''];
disp(str)
end
end
function out = idolThief(ShoeSize, SetColor, Candy)
%Candy=cellstr(Candy)
Candy=strrep(strrep(Candy,' ',''),'''','');
switch (Candy)
case 'SourPatchKids'
if (strcmp(SetColor,'Blue'))
if(ShoeSize==9)
out=['out-> ''The immunity idol thief is'' ','Jessica'];
disp(out);
elseif(ShoeSize==10)
out=['out-> ''The immunity idol thief is'' ','Davie'];
disp(out);
end
end
case 'Butterfinger'
if (strcmp(SetColor,'Green'))
if (ShoeSize==9)
out=['out-> ''The immunity idol thief is'' ','Lyrsa'];
disp(out);
elseif(ShoeSize==10)
out=['out-> ''The immunity idol thief is'' ','Pat'];
disp(out);
end
elseif(strcmp(SetColor,'Red'))
if(ShoeSize==10)
out=['out-> ''The immunity idol thief is'' ','Carl'];
disp(out);
elseif(ShoeSize==9)
out=['out-> ''The immunity idol thief is'' ','Christian'];
disp(out);
end
end
case 'ReesesPeanutButterCups'
if (strcmp(SetColor,'Blue'))
if (ShoeSize==9)
out=['out-> ''The immunity idol thief is'' ','Gabby'];
disp(out);
elseif(ShoeSize==10)
out=['out-> ''The immunity idol thief is'' ','Nick'];
disp(out);
end
elseif(strcmp(SetColor,'Red'))
if(ShoeSize==10)
out=['out-> ''The immunity idol thief is'' ','Elizabth'];
disp(out);
end
end
end
end
function isldStr=islandSimulator2020(isldRes,isldTitle,specRes)
str=cellstr(isldTitle);
index=strcmp(specRes,str(1:numel(str)));
s_m=sum(isldRes(index,:));
if(s_m<20)
isldStr=['isldStr-> THIS AIN''T IT CHIEF'];
disp(isldStr);
elseif(s_m==20)
isldStr=['isldStr-> This island could work, it has ',num2str(s_m), specRes];
disp(isdStr);
else
isldStr=['isldStr-> We''re looking good folks: this island has ',num2str(s_m),' ' specRes];
disp(isldStr);
end
end
rules = [ 'TWWLL';
'LTLWW';
'LWTLW';
'WLWTL';
'WLLWT' ];
Str1='Lightning';
Str2='Earth';
str = chaosChallenge(Str1, Str2, rules);
clear all,clc
ShoeSize=10;
SetColor='Red';
%Candy='Sour Patch Kids';
Candy='Butterfinger';
out=idolThief(ShoeSize, SetColor, Candy);
clear all
isldRes=[4 8 9;
2 5 6;
5 6 10;
6 10 1];
isldTitle=['FOOD';
'WIFI';
'WATR';
'MTLB'];
specRes='FOOD';
isldStr=islandSimulator2020(isldRes,isldTitle,specRes);
function out = secretTunnel(tunnel, dir, steps)
[rows, cols] = size(tunnel);
% First, get the starting position
[row_idx, col_idx] = find(tunnel == 'U');
start = [row_idx, col_idx];
row = tunnel(start(1), :);
col = tunnel(:, start(2));
if dir == 'N'
path = col(start(1)-steps:start(1)-1);
elseif dir == 'S'
path = col(start(1)+1:start(1)+steps);
elseif dir == 'E'
path = row(start(2)+1:start(2)+steps);
elseif dir == 'W'
path = row(start(2)-steps:start(2)-1);
end
wall = find(path == '#');
spike = find(path == '^');
trap = find(path == '+');
cave = find(path == '=');
elements = [wall, spike, trap, cave];
elements = sort(elements);
if length(elements) < 1 % path is clear
out = strcat(['I checked ', dir, ' and the way is clear!']);
return;
else
hazard = path(elements(1));
Nsteps = 0;
if dir == 'N' || dir == 'W'
Nsteps = steps - elements(1)+1;
elseif dir == 'S' || dir == 'E'
Nsteps = elements(1);
end
if hazard == '#'
out = strcat(['I checked ', dir, ' and encountered a(n) wall ', num2str(Nsteps), ' step(s) ahead.']);
return;
elseif hazard == '^'
out = strcat(['I checked ', dir, ' and encountered a(n) spike pit ', num2str(Nsteps), ' step(s) ahead.']);
return;
elseif hazard == '+'
out = strcat(['I checked ', dir, ' and encountered a(n) arrow trap ', num2str(Nsteps), ' step(s) ahead.']);
return;
elseif hazard == '='
out = strcat(['I checked ', dir, ' and encountered a(n) cave-in ', num2str(Nsteps), ' step(s) ahead.']);
return;
end
end
end
function [outStr, pieces] = rulersPuzzle(board)
% Find King position
[Nr, Nc] = size(board);
[row_idx, col_idx] = find(board == 'K');
% Check rows and cols at the position of the King, to see if there is a
% Rook
left_row = board(row_idx, 1:col_idx-1);
right_row = board(row_idx, col_idx+1:end);
top_col = board(1:row_idx-1, col_idx);
bottom_col = board(row_idx+1:end, col_idx);
%% First, check for rooks
Nrooks = 0;
% Check if there are elements of the same team
same = sort([find(left_row == 'N'), find(left_row == 'P'), find(left_row == 'K'), find(left_row == 'p'), find(left_row == 'k'), find(left_row == 'n')]);
rooks = sort(find(left_row == 'r'));
if length(rooks) > 0
if (length(same) < 1) || rooks(end) > same(end)
prox = 0;
if length(same) > 0
prox = same(end);
end
Nrooks = Nrooks + length(find(rooks > prox));
end
end
% Check if there are elements of the same team
same = sort([find(right_row == 'N'), find(right_row == 'P'), find(right_row == 'K'), find(right_row == 'p'), find(right_row == 'k'), find(right_row == 'n')]);
rooks = sort(find(right_row == 'r'));
if length(rooks) > 0
if (length(same) < 1) || rooks(1) < same(1)
prox = Inf;
if length(same) > 0
prox = same(1);
end
Nrooks = Nrooks + length(find(rooks < prox));
end
end
same = sort([find(top_col == 'N'), find(top_col == 'P'), find(top_col == 'K'), find(top_col == 'p'), find(top_col == 'k'), find(top_col == 'n')]);
rooks = sort(find(top_col == 'r'));
if length(rooks) > 0
if (length(same) < 1) || rooks(end) > same(end)
prox = 0;
if length(same) > 0
prox = same(end);
end
Nrooks = Nrooks + length(find(rooks > prox));
end
end
same = sort([find(bottom_col == 'N'), find(bottom_col == 'P'), find(bottom_col == 'K'), find(bottom_col == 'p'), find(bottom_col == 'k'), find(bottom_col == 'n')]);
rooks = sort(find(bottom_col == 'r'));
if length(rooks) > 0
if (length(same) < 1) || rooks(1) < same(1)
prox = Inf;
if length(same) > 0
prox = same(1);
end
Nrooks = Nrooks + length(find(rooks < prox));
end
end
%% Check pawns
pos = [-1,-1;0,-1;1,-1;-1,0;1,0;-1,1;0,1;1,1];
pawn_pos = zeros(size(pos));
pawn_pos(:,1) = row_idx;
pawn_pos(:,2) = col_idx;
pawn_pos = pawn_pos + pos;
pawn_pos(pawn_pos<1) = 1;
pawn_pos(pawn_pos>Nc) = Nc;
idx = sub2ind(size(board), pawn_pos(:,1), pawn_pos(:,2));
Npawns = length(find(board(idx) == 'p'));
%% Check knights
% Find all knights
[rows, cols] = find(board == 'n');
% The move position of a knight is 1 step in one direction and 2 step
% to the other. So the total is: sqrt(1+2^2)= sqrt(5)
% Is there is a knight at a distance of sqrt(5), then it can eat the
% king
my_pos = zeros(length(rows),1);
my_pos(:,1) = row_idx;
my_pos(:,2) = col_idx;
my_pos = my_pos - [rows,cols];
distance = vecnorm(my_pos,2,2);
valid_knights = find(distance == sqrt(5));
Nknights = length(valid_knights);
%% Check king: same procedure as pawns
pos = [-1,-1;0,-1;1,-1;-1,0;1,0;-1,1;0,1;1,1];
king_pos = zeros(size(pos));
king_pos(:,1) = row_idx;
king_pos(:,2) = col_idx;
king_pos = king_pos + pos;
king_pos(king_pos<1) = 1;
king_pos(king_pos>Nc) = Nc;
idx = sub2ind(size(board), king_pos(:,1), king_pos(:,2));
Nkings = length(find(board(idx) == 'k'));
%
ischeck = 0;
pieces = Nrooks + Npawns + Nknights + Nkings;
if pieces > 0
ischeck = 1;
end
if ischeck == 1
outStr = 'You are in check!';
else
outStr = 'You are not in check!';
end
pieces = Nrooks + Npawns + Nknights + Nkings;
end