How do I multiples of a number in an array
조회 수: 26 (최근 30일)
이전 댓글 표시
How do I create array with following properties: For the first row, each element is 8 times the corresponding element of x. For the second row, each element is 2 times the square of the corresponding element of x followed by a subtraction 1.
댓글 수: 0
채택된 답변
DGM
2021년 7월 14일
Depends how far you want to go with it. You could leverage implicit array expansion to do it in a way that might make a more general solution a bit easier.
x = 1:10;
k = [8; 2];
p = [1; 2];
a = [0; 1];
A = k.*x.^p - a
댓글 수: 2
dpb
2021년 7월 15일
I had thought of polyval but was/is unfortunate it isn't internally vectorized --
x=1:3;
b=[0 8 0;2 0 -1];
>> [polyval(b(1,:),x);polyval(b(2,:),x)]
ans =
8.00 16.00 24.00
1.00 7.00 17.00
>> cell2mat(arrayfun(@(i)polyval(b(i,:),x),1:size(b,1),'uni',0).')
ans =
8.00 16.00 24.00
1.00 7.00 17.00
>>
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!