Question

how to modify code in matlab to have a calculation linked to various names? for example...

how to modify code in matlab to have a calculation linked to various names?

for example if katie weighs 120 lbs and her density is 2.2, but abby weighs 140 lbs and her density is 3.4

the output i want is volume

how do i associate weight and density to a name? so if the user were to input “katie” matlab would know that the weight and density are 120 and 2.2

Homework Answers

Answer #1

USE ORDERED ARRAYS/CELLS FOR THE NAMES, WEIGHTS AND DENSITIES THEN COMPARE THE INPUT (NAME) WITH ALL THE NAMES, IF A MATCH IS FOUND, LOOK UP THE CORRESPONDING VALUES FOR WEIGHT AND DENSITY

'

strcmpi - compares 2 strings (not case sensitive) and returns true (1) if the two strings are equal, false (0) otherwise

Consider the matlab program below:

'

'

____________________________________

clc,clear

%%
% define the variables in [ordered] arrays/cells
names = {'katie','abby'}; % names
w = [120 140]; % weight
d = [2.2,3.4]; % density

%%
% get name from input
name = input('Enter name: ','s');

%%
% search for the name entered and if found, look up the weight and density
for i=1:length(names) % check all names
if(strcmpi(names(i),name)) % compare the name with all names
% if found, do something with the weight and density
weight = w(i)
density = d(i)
volume = weight/density
end
end


------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions