Reputation: 33
I'm new in MATLAB and I need help. I have to complete a code that initializes a matrix, but I don't know where to start.
The matrix must be in this form:
Where Yi = i/m
for i = 1, ..., m
, with m=6
.
The code to complete is:
m = 6;
A = [1:m;
1:m;
...];
A = A/m;
Upvotes: 1
Views: 80
Reputation: 6863
You can use implicit expansion.
m = 6;
A = ((1:m).'/m).^(0:m);
Explanation. First make a column vector with the values y1
till ym
.
c = (1:m).'/6;
Then make a matrix, in which the first column will be c
to the power of 0, the second c
to the power of 1, etc.
You can do this easily with implicit expansion, take your column vector and raise to the power (element wise) of the row vector containing 0:m
.
A = c.^(0:m);
Upvotes: 4
Reputation: 101558
Here are two approaches:
vander
+ fliplr
A = fliplr(vander((1:m)/m));
arrayfun
+ vertcat
C = arrayfun(@(x) x.^(0:m),(1:m)/m, 'UniformOutput', false);
A = vertcat(C{:});
Result
>> A
A =
1.000000000 0.166666667 0.027777778 0.004629630 0.000771605 0.000128601 0.000021433
1.000000000 0.333333333 0.111111111 0.037037037 0.012345679 0.004115226 0.001371742
1.000000000 0.500000000 0.250000000 0.125000000 0.062500000 0.031250000 0.015625000
1.000000000 0.666666667 0.444444444 0.296296296 0.197530864 0.131687243 0.087791495
1.000000000 0.833333333 0.694444444 0.578703704 0.482253086 0.401877572 0.334897977
1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000 1.000000000
Upvotes: 2