Loading

matlab 向量操作作业

写出下列语句的计算结果及作用

clear    清除所有变量

clc    清屏

A = [2 5 7 1 3 4];    创建行向量并赋值

odds = 1:2:length(A);    冒号操作符,创建从1到length(A),,步数为2的行向量

disp('odd values of A using predefined indices')    输出字符串

A(odds)    输出A中odds中元素对应的下标的值

disp('odd values of A using anonymous indices')    输出字符串

A( 1:2:end)    从第一位到最末位,步数为2,输出元素

disp('put evens into odd values in a new array')    输出字符串

B(odds) = A(2:2:end)    把B中odds对应的元素赋值为A(2:2:end)输出的值(把A中偶数位置的元素放入B中奇数位置)

disp('set the even values in B to 99') 输出字符串

B(2:2:end) = 99    将B中下标为偶数的元素赋值为99

 

disp('find the small values in A')    输出字符串

small = A < 4    产生一个数组,A中每个元素若<4,在新数组中相应的位置赋值为1,否则为0

 

disp('add 10 to the small values')    输出字符串

A(small) = A(small) + 10    A中符合small条件的元素自加10

 

disp('this can be done in one ugly operation')    输出字符串

A(A<4) = A(A<4)+ 10    A中<4的元素自加10

 

posted @ 2020-06-17 16:18  sq800  阅读(578)  评论(0编辑  收藏  举报