How can one define this variable in an elegant way?

조회 수: 3 (최근 30일)
alpedhuez
alpedhuez 2020년 6월 1일
댓글: alpedhuez 2020년 6월 2일
I want to define a variable x that is 300*1 vector. For the first 150 element, it is defined as an innerproduct of the first 150 element of a that is 300*1 and b that is 150*1. For the remaining 150 element, it will be defined by polyval(s1) of the remaining 150 element of a. I wrote
x=zeros(300,1)
for i=1:150
x(i,1)=a(i,1)*b(i,1)
end
for i=151:300
x(i,1)=polyval(s1,a(i,1))
end
But how can one write it in a more compact way?

채택된 답변

David Hill
David Hill 2020년 6월 1일
x(1:150)=a(1:150).*b;
x(151:300)=polyval(s1,a(151:300));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by