How do I multiples of a number in an array

26 Ansichten (letzte 30 Tage)
Emmanuel Lebile
Emmanuel Lebile am 14 Jul. 2021
Kommentiert: dpb am 15 Jul. 2021
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.

Akzeptierte Antwort

DGM
DGM am 14 Jul. 2021
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
A = 2×10
8 16 24 32 40 48 56 64 72 80 1 7 17 31 49 71 97 127 161 199
  2 Kommentare
Emmanuel Lebile
Emmanuel Lebile am 15 Jul. 2021
Job welldone.Thanks
dpb
dpb am 15 Jul. 2021
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
>>

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 14 Jul. 2021
Bearbeitet: dpb am 14 Jul. 2021
Well, what have you tried? Follow the recipe given --
y=[8x;2*x.^2-1]; % x is vector
If size(x) --> 2,N, then will need to subscript the above appropriately or clarify what the definition really means.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by