
Mjukstartar lite med en enkel(?).
Jag har en 1*n-matris "A" med värden av datatypen double. Nu vill jag subtrahera 1+n*x från varje cell (där n alltså är cellnumret med 1 som första cell). Hur gör jag enklast?
Aha!Andax skrev:Du skriver t.ex.: B = A - (1+(1:n)*x);
1:n är en 1*n matris vars celler har värdet från 1 till n.
Nu fattade jag att x bara är en konstant. Är x t.ex. En vektor med längden n kan man ersätta (1:n)*x med (1:n).*x för elementvis multiplikation.
Kod: Markera allt
>> n=(1:10).^2
n =
1 4 9 16 25 36 49 64 81 100
>> diff(n)
ans =
3 5 7 9 11 13 15 17 19
Kod: Markera allt
>> n=1:9
n =
1 2 3 4 5 6 7 8 9
>> nrev = [n(1) n(end:-1:2)]
nrev =
1 9 8 7 6 5 4 3 2
Kod: Markera allt
>> n=(1:10).^2
n =
1 4 9 16 25 36 49 64 81 100
>> diffn = diff(n)
diffn =
3 5 7 9 11 13 15 17 19
>> cumsum([n(1) diffn])
ans =
1 4 9 16 25 36 49 64 81 100
[X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.
Kod: Markera allt
>> x = [1,2;3,4]
x =
1 2
3 4
>> y = [5;6]
y =
5
6
>> z=[x,y]
z =
1 2 5
3 4 6