matlab怎么写[1 -1 1 -1 .]向量在做这道题:Make the vector lin =[1 2 ⋯ 20] (the integers from 1 to 20),and then makeevery other value in it negative to get lin =[1 −2 3 −4 ⋯ −20]

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 18:36:33

matlab怎么写[1 -1 1 -1 .]向量在做这道题:Make the vector lin =[1 2 ⋯ 20] (the integers from 1 to 20),and then makeevery other value in it negative to get lin =[1 −2 3 −4 ⋯ −20]
matlab怎么写[1 -1 1 -1 .]向量
在做这道题:
Make the vector lin =[1 2 ⋯ 20] (the integers from 1 to 20),and then make
every other value in it negative to get lin =[1 −2 3 −4 ⋯ −20]

matlab怎么写[1 -1 1 -1 .]向量在做这道题:Make the vector lin =[1 2 ⋯ 20] (the integers from 1 to 20),and then makeevery other value in it negative to get lin =[1 −2 3 −4 ⋯ −20]
lin=1:20;
for n=1:length(lin)
lin(n)=lin(n)*(-1).^(n+1)
end
lin
结果为:
Columns 1 through 17
1 -2 3 -4 5 -6 7 -8 9 -10 11 -12 13 -14 15 -16 17
Columns 18 through 20
-18 19 -20