Vous êtes sur la page 1sur 3

# 1) Create a vector of the even whole numbers between 31 and 75.

clear,clc
X=31:75;
Even=find(rem(X,2)==0);
MyEven=X(Even)
2) Write a script AveNegNum that will repeat the process of prompting the user for negative
numbers, until the user enters a zero or positive number, as just shown. Instead of echo printing
them, however, the script will print the average (of just the negative numbers). If no negative
numbers are entered, the script will print an error message instead of the average.
Examples of executing this script follow:
>> AveNegNum
Enter a negative number: 5
No negative numbers to average.
>> AveNegNum
Enter a negative number: -8
Enter a negative number: -3
Enter a negative number: -4
Enter a negative number: 6
The average was -5.00
clear, clc
X=[];
while (1)
NegNum=input('Enter a negative number: ');
if(NegNum<0)
X=[X,NegNum];
else
if(length(X)==0)
disp('No negative numbers to average');
break
else
fprintf('The average was %2.2f',mean(X));
break
end
end
end

Metal
Symbol
Atomic
Atomic
Number
Weight

Density
g/cm3

Crystal
Structure

Aluminum
Copper
Iron
Molybdenum
Cobalt

Al
Cu
Fe
Mo
Co

13
29
26
42
27

26.98
63.55
55.85
95.94
58.93

2.71
8.94
7.87
10.22
8.9

FCC
FCC
BCC
BCC
HCP

## (a) Create the following arrays:

Store the name of each metal into an individual character array, and
store all these character arrays into a cell array.
Store the symbol for all these metals into a single padded character
array.
Store the atomic number into an integer array.
Store the atomic weight into a double-precision numeric array.
Store the density into a single-precision numeric array.
Store the structure into a single padded character array.
(b) Group the arrays you created in part (a) into a single cell array.
(c) Extract the following information from your cell array:
Find the name, atomic weight, and structure of the fourth element in
the list.
Find the names of all the elements stored in the array.
Find the average atomic weight of the elements in the table.
(Remember, you need to extract the information to use in your calculation
from the cell array.)

clc,clear
%%%%%Part a
M1=['Aluminum '];
M2=['Copper'];
M3=['Iron'];
M4=['Molybdenum'];
M5=['Cobalt'];
Names={M1;M2;M3;M4;M5};
Symbols=['FCC';'FCC';'FCC';'BCC';'HCP'];
AtomicNum=[13;29;26;42;27]; %%range is up to 127
AtomicNum=int8(AtomicNum);
AtomicWeight=[26.98;63.55;55.85;95.94;58.93]; % Copy and paste
from Excel
Density=single([2.71;8.94;7.87;10.22;8.9]);
Structure=char(['FCC';'FCC';'BCC';'BCC';'HCP']); %%%Create all
with semicolons
%%%%%%%Part B
Metals={Names,Symbols,AtomicNum,AtomicWeight,Density,Structure};
%%%%%%%%Part C
Metals{1,1}(4,1)%%%Name
Metals{1,4}(4,1)%atomic weight
Metals{1,6}(4,1) % This will return only the first character: B
Metals{1,6}(4,1:end) % This will return the whole character BCC

%%%%%%%%Names
Metals{1,1}
%%%%%%%%%%Average atomic weight
mean(Metals{1,4})

4) Assume there is a file called datavals.dat containing results recorded from an experiment.
However, some were recorded erroneously. The numbers are all supposed to be positive. Write a
script that reads from this file into a matrix, then prints the sum from each row of only the positive
numbers. We will assume that the file contains integers, but will not assume how many lines are in
the file or how many numbers per line (although we will assume that there are the same number of
integers on every line).

clc, clear