Suppose I have 4 letters and I want to arrange them in 3 places (repetition allowed), so I would have 43=64 possible permutations. How can I compute and print them?
Simplifying Amro's answer, you could use this:
%// Sample data
x = 'ABCD'; %// Set of possible letters
K = 3; %// Length of each permutation
%// Create all possible permutations (with repetition) of letters stored in x
C = cell(K, 1); %// Preallocate a cell array
[C{:}] = ndgrid(x); %// Create K grids of values
y = cellfun(#(x){x(:)}, C); %// Convert grids to column vectors
y = [y{:}]; %// Obtain all permutations
Matrix y should store the permutations you're after.
How about the function N_PERMUTE_K from the File Exchange?
An intuitive one-liner:
unique(nchoosek(repmat('ABCD', 1,4), 3), 'rows')
Although nice-looking, it's slow and inefficient. Don't use it for large data sets.
Pseudocode solution:
Generate the (base ten) numbers 0 to 63.
Change them to base 4, which only has the digits 0, 1, 2, and 3.
Convert numbers to letters.
The actual Matlab code is left as an exercise for the student.
Related
Generating evenly space can use linspace, but I wonder if I can vectorize it. What I mean is as follow:
Given an input vector, say [1 2], I want to generate a 2X6 matrix such that:
in the first row, the entries are [0:0.2:1]
the entries for the second row are [0:0.4:2]
In general, the input vector may not be known, it can change from [1 2] to [1:3:10] or other vectors. However, the first column much be a zero vector and the number of columns can be treated to be the known in advanced.
I do not want to write it using a for loop if possible.
Assuming A = [x, y], you can generate the desired 2 x 6 matrix M as follows:
B = A/A(1);
M = B.'*linspace(0, A(1), 6);
I am trying to generate a 2D data set with the following parameters:
x= N(-5,1) y= N(0,1) n= 1000
Where N(mean, std dev) and n = number of samples.
I tried:
x = normrnd(-5, 1, [100,10])
y = normrnd(0,1,[100,10])
to generate a 100 x 10 array with the appropriate values. I now need to find a way to output the values from these two arrays into an N(x,y) format that can be analyzed by Weka. Any suggestions on how to do this would be appreciated.
Given your comments, you want to generate a N x 2 matrix where each row is a pair of values that both come from different normal distributions.
You can either generate the 2D matrices of each separately and unroll them into single vectors and concatenate them both.... or the simplest way is to just generate 100 x 10 = 1000 elements in a 1D vector from each distribution and concatenate these together.
Method #1 - 2D matrix unrolling
x = normrnd(-5, 1, [100,10]);
y = normrnd(0, 1, [100,10]);
N = [x(:) y(:)];
Method #2 - 1D vector concatenation
x = normrnd(-5, 1, [1000,1]); %// Change
y = normrnd(0, 1, [1000,1]); %// Change
N = [x y];
If you wish to write this to a CSV file, where you have a pair of x,y values separated by a comma and you have Class_A at the end, a call to fopen to open up a file for writing, fwrite to write our stuff to the file and fclose to finally close the file is needed. You also require that the digits are 3 digits of precision. Something like this comes to mind:
f = fopen('numbers.csv', 'w'); %// Open up the file
fprintf(f,'%.3f,%.3f,Class_A\n', N.'); %'// Write the data
fclose(f); %// Close the file
It's important to look at the second statement carefully. Note that I'm writing the transpose of N because MATLAB writes values in column-major order. This means that if you want the rows to be written to the file, you have to transpose the matrix to do that. numbers.csv is what the file is called when it is written. If you examine this file now, you'll see that it's in the form of x,y,Class_A where x,y is a pair of values from both normal distributions.
You can use mvnrnd(mu_vector, sigma_matrix)
mu = [-5;0];
Sigma = [1,0;0,1];
n = 1000;
X = mvnrnd(mu, Sigma, n);
I need to generate all square matrices of order n with given properties.
Matrices are symmetric.
Entries are 0 and 1.
Diagonal elements are zeros.
I am using Matlab2012b. Can you help me with the code?
I was trying to write it down. It needs a long sequences of for loops. Any simpler technique?
Try this:
N = 4; %// matrix size
M = (N^2-N)/2; %// number of values to fill in each matrix
P = 2^M; %// number of matrices
x = dec2bin(0:P-1)-'0'; %// each row contains the values of a matrix, "packed" in a vector
result = NaN(N,N,P); %// preallocate
for k = 1:P
result(:,:,k) = squareform(x(k,:)); %// unpack values
end
The matrices are result(:,:,1), result(:,:,2) etc.
I am looking to find all possible linear combinations of a set of matrices over GF(2). I know the number of matrices, k, and they are all the same dimension, stored in a 3D array, C(:,:,i) for the ith matrix. Because I'm working over GF(2), all the coefficients of the linear combination must be in {0,1}. I would like to generate each of the 2^k possible sums so that I may test the resulting matrix for a required property. There are many posts about generating all combinations of elements of matrices or vectors, but I am looking to generate all linear combinations of the matrices as a whole.
Many Thanks!
Here is an example:
%# some data to work with
sz = [4 3];
k = 6;
C = rand([sz k]);
%# coefficients [0,0,0,0,0,0] to [1,1,1,1,1,1]
p = (dec2bin(0:2^k-1) == '1');
%# generate all linear combinations with the above coefficients
for i=1:size(p,1)
%# C(:,:,1)*p(i,1) + C(:,:,2)*p(i,2) + ... + C(:,:,k)*p(i,k)
linComb = sum(bsxfun(#times, permute(p(i,:),[1 3 2]), C),3);
%# do something interesting with it ...
end
I have a matrix that does not happen to have dimensions that are multiples of 3 or it might.
How can we divide the entire image into blocks of 3*3 matrices.
(Can ignore the last ones which does not come under the 3*3 multiples.
Also, the 3*3 matrices can be be saved in arrays.
a=3; b=3; %window size
x=size(f,1)/a; y=size(f,2)/b; %f is the original image
m=a*ones(1,x); n=b*ones(1,y);
I=mat2cell(f,m,n);
I have never used mat2cell to divide matrices, and thinking about it now it seems like a really good idea. As I don't have MATLAB here in this computer, I'll describe the way I do it, which does not involve mat2cell.
Ignoring the last columns and rows is easy:
d = 3; % the dimension of the sub matrix
[x,y] = size(f);
% perform integer division by three
m = floor(x/d);
n = floor(y/d);
% find out how many cols and rows have to be left out
m_rest = mod(x,d);
n_rest = mod(y,d);
% remove the rows and columns that won't fit
new_f = f(1:(end-m_rest), 1:(end-n_rest));
% this steps you won't have to perform if you use mat2cell
% creates the matrix with (m,n) pages
new_f = reshape( new_f, [ d m d n ] );
new_f = permute( new_f, [ 1 3 2 4 ] );
Now you can access the sub-matrices like this:
new_f(:,:,1,1) % returns the 1st one
new_f(:,:,3,2) % returns the one at position [3,2]
If you'd like to use mat2cell to do that, you could do something like the following:
% after creating new_f, instead of the reshape, permute
cells_f = mat2cell(new_f, d*ones(1,m), d*ones(1,n));
Then you would access it in a different way:
cells_f{1,1}
cells_f{3,2}
The cell approach I cannot test because I don't have MATLAB on this PC, but if I can recall the usage of mat2cell correctly, it should work fine.
Hope it helps :)