I create my PN generator with this code:
h=commsrc.pn('GenPoly',gfprimdf(3), 'InitialStates',[1 0 0], ...
'CurrentStates', [1 0 0], 'Mask',[0 0 1], 'NumBitsOut',1)
And this is the GF polynomial of my PN generator:
>> gfpretty(h.GenPoly)
3
1 + X + X
The current states of h is:
>> h.CurrentStates
ans =
1 0 0
looking at the GF polynomial, I think the next statue of h should be [0 1 0]. But Matlab turns the next states of h into [1 1 0] not the expected value [0 1 0].
>> generate(h);
>> h.CurrentStates
ans =
1 1 0
gfprimdf(...) generates the generator polynomial in the order 1+a1*x+a2*x^2+a3*x^3+...+x^N and represents this as a vector
[1 a_1 a_2 ... a_N-1 1]
(ascending order of polynomial powers). However, commsrc.pn expects this vector to be in descending order.
Thus, in your example, the generator polynomial which is really used by commsrc.pn is 1+x^2+x^3, and not 1+x+x^3 as intended. If you instead use
h=commsrc.pn('GenPoly',[1 0 1 1],'InitialStates',[1 0 0],'CurrentStates',[1 0 0],'Mask',[0 0 1],'NumBitsOut',1);
the state after generating one output bit results in the expected state.
Related
Working on an assignment from Coursera Machine Learning. I'm curious how this works... From an example, this much simpler code:
% K is the number of classes.
K = num_labels;
Y = eye(K)(y, :);
seems to be a substitute for the following:
I = eye(num_labels);
Y = zeros(m, num_labels);
for i=1:m
Y(i, :)= I(y(i), :);
end
and I have no idea how. I'm having some difficulty Googling this info as well.
Thanks!
Your variable y in this case must be an m-element vector containing integers in the range of 1 to num_labels. The goal of the code is to create a matrix Y that is m-by-num_labels where each row k will contain all zeros except for a 1 in column y(k).
A way to generate Y is to first create an identity matrix using the function eye. This is a square matrix of all zeroes except for ones along the main diagonal. Row k of the identity matrix will therefore have one non-zero element in column k. We can therefore build matrix Y out of rows indexed from the identity matrix, using y as the row index. We could do this with a for loop (as in your second code sample), but that's not as simple and efficient as using a single indexing operation (as in your first code sample).
Let's look at an example (in MATLAB):
>> num_labels = 5;
>> y = [2 3 3 1 5 4 4 4]; % The columns where the ones will be for each row
>> I = eye(num_labels)
I =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
>> Y = I(y, :)
Y =
% 1 in column ...
0 1 0 0 0 % 2
0 0 1 0 0 % 3
0 0 1 0 0 % 3
1 0 0 0 0 % 1
0 0 0 0 1 % 5
0 0 0 1 0 % 4
0 0 0 1 0 % 4
0 0 0 1 0 % 4
NOTE: Octave allows you to index function return arguments without first placing them in a variable, but MATLAB does not (at least, not very easily). Therefore, the syntax:
Y = eye(num_labels)(y, :);
only works in Octave. In MATLAB, you have to do it as in my example above, or use one of the other options here.
The first set of code is Octave, which has some additional indexing functionality that MATLAB does not have. The second set of code is how the operation would be performed in MATLAB.
In both cases Y is a matrix generated by re-arranging the rows of an identity matrix. In both cases it may also be posible to calculate Y = T*y for a suitable linear transformation matrix T.
(The above assumes that y is a vector of integers that are being used as an indexing variables for the rows. If that's not the case then the code most likely throws an error.)
Given an sparse matrix A in MATLAB and the mean for the nonzero elements in its columns m, is there anyway to subtract the nonzero elements in each column from the mean of each column and avoid looping over columns?
I am looking for efficient solutions. Using 'bsxfun' could be one solution if it is possible to use.
Thanks
You can use the second output of find to get the column indices; use those to index into m to do the subtraction; and put the results back into A using logical indexing:
A = sparse([0 0 0 0; 1 0 3 2; 2 1 0 5]); %// example data
m = [1.5 1 3 3.5]; %// vector of mean of nonzero elements of each column
m = m(:);
[~, jj, vv] = find(A);
A(logical(A)) = vv - m(jj);
Original A:
>> full(A)
ans =
0 0 0 0
1 0 3 2
2 1 0 5
Final A:
>> full(A)
ans =
0 0 0 0
-0.5000 0 0 -1.5000
0.5000 0 0 1.5000
I want to replace value of n-th column of a matrix, based on a vector values, in a vectorized way.
Input:
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
Vector:
[2]
[4]
[1]
[3]
Expected output:
[0 1 0 0]
[0 0 0 1]
[1 0 0 0]
[0 0 1 0]
Matlab code with for-loop:
A = zeros(4,4);
b = [2; 4; 1; 3];
for row=1:4
A(row, b(row)) = 1;
endfor
Matlab code with sub2ind:
A = zeros(4,4);
b = [2; 4; 1; 3];
c = [[1:length(b)]' b];
A(sub2ind(size(A), c(:,1), c(:,2))) = 1;
Is there more vectorized way in Matlab? Thank you.
You can use the raw version of sub2ind to solve it in a vectorized manner -
A( (b(:)-1)*size(A,1) + [1:numel(b)]' ) = 1;
How it works: Since elements of b are the column indices and MATLAB follows column-major indexing, so we need to multiply each such column index with the number of rows in A to get us the number of elements before starting that column, which would be (b(:)-1)*size(A,1). Then, add the corresponding row indices i.e. [1:numel(b)]' to give us the final linear indices, same as the ones produced by sub2ind. Finally, index into A with those linear indices and set them to all 1's as per the question's requirement.
This code works perfectly in Octave, but not in Matlab. But why? Is there any workaround? Thanks.
a = [0; 5; 10];
b = [3 5 7];
a >= b
Octave behavior:
0 0 0
1 1 0
1 1 1
Matlab behavior:
Error using >
Matrix dimensions must agree.
Use bsxfun:
>> bsxfun( #ge, a, b )
ans =
0 0 0
1 1 0
1 1 1
bsxfun is so much FUN!
Im looking to create a matrix of rank k.
The dimension of the matrix is m x n. The input k satisfies that condition that k < min(m,n).
It's not really so clear what you are aiming for.
But in order to create a matrix B with specific rank k, from a matrix A (with rank at least k), you may like to utilize svd and proceed like:
>>> A= rand(7, 5);
>>> rank(A)
ans = 5
>>> [U, S, V]= svd(A);
>>> k= 3;
>>> B= U(:, 1: k)* S(1: k, 1: k)* V(:, 1: k)';
>>> rank(B)
ans = 3
Well, a trivial method is to produce a matrix that looks like:
1 0 0 0 0
0 1 0 0 0
0 0 1 1 1
0 0 0 0 0
i.e. k columns of the identity matrix, then repeat the last column n-k times (or m-k times, depending on orientation).
A matrix of rank 1 can be created by the outer product of two vectors, for example:
A = randn(10,1) * randn(1,10);
Add together k of these and you will have a matrix of rank k. Like this:
>> A = zeros(10);
>> for i = 1:4, A = A + randn(10,1) * randn(1,10); end
>> rank(A)
ans = 4