rows=3;
columns=5;
for j =1:rows
for i =1:columns
fprintf('*');
end
fprintf('\n')
>> test
*****
*****
*****
end
rows=10;
for j =1:rows
for i =1:j
fprintf('*');
end
fprintf('\n');
end
>> test
*
**
***
****
*****
******
*******
********
*********
**********
制作一个4行5列的矩阵,矩阵中每个元素的大小等于行数i×列数j
rows=4;
columns=5;
mat=NaN(rows,columns);
for i = 1:rows
for j=1:columns
mat(i,j)=i*j;
end
end
>> test
>> mat
mat =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
输出乘法口诀表
for i = 1:9
for j=1:i
fprintf(' %1d × %1d = %-2d |',j,i,i*j);
end
fprintf('\n');
end
![[Pasted image 20230204171545.png]]
把如图所示矩阵保存在datavals.dat文件中,把每行的非负数相加求和
![[Pasted image 20230204175152.png]]
%命令行窗口
>> mat=[33 -11 2;4 5 9;22 5 -7;2 11 3]
mat =
33 -11 2
4 5 9
22 5 -7
2 11 3
>> save datavals.dat mat -ascii
%m文件
load datavals.dat;
[r,c] = size(datavals);
for row = 1:r
runsum=0;
for col =1:c
if datavals(row,col)>=0
runsum=runsum+datavals(row,col);
end
end
fprintf('The sum for row %d is %d \n',row,runsum)
end
%命令行窗口
>> sumonlypos
The sum for row 1 is 35
The sum for row 2 is 18
The sum for row 3 is 27
The sum for row 4 is 16
输入一个数,使程序输出一个整数,使得该整数的阶乘大于输入的那个数
%m文件
function facGt = factGtHigh(high)
i=0;
fac=1;
while fac<=high
i=i+1;
fac=fac*i;
end
facGt = i;
end
%命令行窗口
>> factGtHigh(1E4)
ans =
8
找数组中第一次遇到-99的位置
%命令行窗口
>> mat=[3.1 11 5.2 8.9 -99 4.4 62]
mat =
3.1000 11.0000 5.2000 8.9000 -99.0000 4.4000 62.0000
>> save newvec.dat mat -ascii
%m文件
load newvec.dat;
i=1;
while newvec(i)~=-99
i=i+1;
end
%命令行窗口
>> findvalwhile
>> whos
Name Size Bytes Class Attributes
i 1x1 8 double
mat 1x7 56 double
newvec 1x7 56 double
>> i
i =
5
>> newvec(i)
ans =
-99
把-99之前的数据点在图上描绘出来
%m文件
load newvec.dat;
i=1;
while newvec(i)~=-99
i=i+1;
end
validData=newvec(1:i-1);
plot(validData,'ko');
xlabel('Reading #');
ylabel('Weight (pounds)');
title('Valid Data Set');
在这里插入图片描述
在数组中多次出现一个数时找到该数的位置
%命令行窗口
>> mat=[3.1 11 5.2 8.9 -99 4.4 62 -99 5.5 5.5 -99 2.3 34];
>> save newvec.dat mat -ascii
%m文件
load newvec.dat;
i=find(newvec==-99,3,'first');
%命令行窗口
>> findvalwhile
>> i
i =
5 8 11
输入负数直到用户输入正数时为止
%m文件
inputnum=input('Enter a positive number:');
while inputnum<=0
fprintf('You enter a %f \n',inputnum);
inputnum=input('Enter a positive number:');
end
fprintf('OK!\n');
%命令行窗口
>> wileposnum
Enter a positive number:-1
You enter a -1.000000
Enter a positive number:-2
You enter a -2.000000
Enter a positive number:9
OK!
输入正数到输入一个负数时停止,统计输入正数的个数
%m文件
counter=0;
inputnum=input('Enter a positive number:');
while inputnum>0
fprintf('You enter a %f \n',inputnum);
counter=counter+1;
inputnum=input('Enter a positive number:');
end
fprintf('Thanks,you entered %d positive numbers.\n',counter);
%命令行窗口
>> countposnum
Enter a positive number:1
You enter a 1.000000
Enter a positive number:2
You enter a 2.000000
Enter a positive number:3
You enter a 3.000000
Enter a positive number:4
You enter a 4.000000
Enter a positive number:5
You enter a 5.000000
Enter a positive number:6
You enter a 6.000000
Enter a positive number:-7
Thanks,you entered 6 positive numbers.
要求输入负数,直到输入一个非负数,输出之前输入的负数的平均数。如果一个正数都没有输入,程序输出一个错误代码
%m文件
inputnum=input('Enter a negative number:');
if inputnum<0
sum=0;
counter=0;
while inputnum<0
fprintf('You enter a %f\n',inputnum);
sum=sum+inputnum;
counter=counter+1;
inputnum=input('Enter a negetive number:');
end
fprintf('Thanks.The average is %f \n',sum/counter);
else
fprintf("No negetive numbers to average.\n");
end
%命令行窗口
>> avenegnum
Enter a negative number:-1
You enter a -1.000000
Enter a negetive number:-2
You enter a -2.000000
Enter a negetive number:-3
You enter a -3.000000
Enter a negetive number:-4
You enter a -4.000000
Enter a negetive number:-5
You enter a -5.000000
Enter a negetive number:13
Thanks.The average is -3.000000
输入3个正数,输入非正数时提示用户
n=3;
for i=1:n
inputnum=input('Enter a positive number:');
while inputnum <=0
fprintf('You enter a %f\n',inputnum);
inputnum=input('Enter a positive number:');
end
fprintf('OK!\n');
end
输入3个正数,输入非正数时提示用户,每输入一个正数提示用户输入的是第几个数
n=3;
for i=1:n
inputnum=input(['Enter a positive number [#' num2str(i) ']:']);
while inputnum <=0
fprintf('You enter a %f\n',inputnum);
inputnum=input(['Enter a positive number [#' num2str(i) ']:']);
end
fprintf('OK!\n');
end
run section
tic toc测时