How to generate all possible combinations of K instances of N objects in Matlab? [duplicate] - matlab

This question already has answers here:
Generate a matrix containing all combinations of elements taken from n vectors
(4 answers)
Closed 8 years ago.
How to generate all possible combinations of K instances of N objects in Matlab?
For example, if we have N=3 objects A, B and C, and wish to generate combinations of K=2 instances, we should get
AA
AB
AC
BA
BB
BC
CA
CB
CC
I.e. this is how figures in a number combinate.

You can use fullfact:
fullfact([3 3])
This will produce all the combinations of 3 elements in the first row and 3 elements in the second row, resulting with:
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3

Using allcomb from matlab file exchange:
allcomb(['A','B','C'],['A','B','C'])
download
A more generalized method:
s='A':'D'
k=3
e=repmat({s},1,k)
allcomb(e{:})

Related

How to transpose rows into columns in specific order in MATLAB? [duplicate]

This question already has an answer here:
How to convert the elements of a matrix into a single vector
(1 answer)
Closed 5 years ago.
I have a data (the name of data is testdata) in work-space 60x5 double. I have 60 different measurement and 5 samples. To calculate in R, I need to save them in an order that my R code can use them. What I need to do is transposing every 5 rows of each column to one row and adding the next transposed rows under those 5 values. You can find an image of what I want to do. As seen in the image, black rectangle should be transposed to column, then red rectangle transpose and added under the columns which is already used for the first step. I need to do this 60 times so at the end it should be 300x1 double. I hope someone can help me to solve this issue out. Thank you for your time and help.
Best Regards,
See if this is what you want:
A = magic(3); % example matrix
B = A.'; % transpose
B = B(:); % linearize in column-major order
This transforms
A =
8 1 6
3 5 7
4 9 2
into
B =
8
1
6
3
5
7
4
9
2
Reshape will do exactly what you are looking for:
A = magic(5); % Example matrix
B = reshape(A',[],1); % Reshapes the matrix to one vector.

Matlab - equivalent of R's rep() with times argument [duplicate]

This question already has answers here:
Repeat copies of array elements: Run-length decoding in MATLAB
(5 answers)
Closed 6 years ago.
I am wondering what is the fastest way to achieve in Matlab what in R I would achieve with the rep() function with the times argument, e.g.
v1=1:5;v2=5:1;out=rep(v1,times=v2);out
# 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5
i.e. replicate each element in vector v1 a number of times given by the corresponding element in vector v2. Any thoughts?
You can use repmat or repelems, e.g.
z = repelems(x,[1:4;rep])

Access to row i of a specific matrix [duplicate]

This question already has answers here:
Combinations from a given set without repetition
(2 answers)
Closed 7 years ago.
I want to access to row i of A=nchoosek(1:m,n). This command of MATLAB is very time consuming, specially for large m. So, I do not want to construct whole of A. I want to build just rowi of A.
Although, it seems that my question is duplicate of "Combinations from a given set without repetition", but they are different.
That answer did not cover different columns. It is just get acceptable results for A = nchoosek(M,2). I want to find A (i,:), where A= nchoosek(1:m,n), for given i, m, and n.
This answer answers the original version of the question, not the updated version
This is exactly what nchoosek does, when you input a vector.
nchoosek([1:n],m)
.
>> m=2
m =
2
>> n=5
n =
5
>> nchoosek([1:n],m)
ans =
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5

How to extend the rows of a matrix in MATLAB filling the added rows with the first row's values efficiently [duplicate]

This question already has answers here:
Building a matrix by merging the same row vector multiple times
(2 answers)
Closed 8 years ago.
I have a matrix myVel that is of size [1 501] meaning 1 row and 501 columns.
I want to extend this matrix so that the matrix will be of size [N 501], where N is an arbitrary number.
Each of the values in the columns need to be the same (meaning that all the values in the first column are all say, x and all of the values in the second column are say, y and so on).
This means that each row would consist of the same values.
How can I achieve this efficiently?
Divakar's solution is one way to do it, and the link he referenced shows some great ways to duplicate an array. That post, however, is asking to do it without the built-in function repmat, which is the easiest solution. Because there is no such restriction for you here, I will recommend this approach. Basically, you can use repmat to do this for you. You would keep the amount of columns the same, and you would duplicate for as many rows as you want. In other words:
myVelDup = repmat(myVel, N, 1);
Example:
myVel = [1 2 3 4 5 6];
N = 4;
myVelDup = repmat(myVel, N, 1);
Output:
>> myVel
myVel =
1 2 3 4 5 6
>> myVelDup
myVelDup =
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
In general, repmat is called in the following way:
out = repmat(in, M, N);
in would be a matrix or vector of values you want duplicated, and you would want to duplicate this M times horizontally (rows) and N times vertically (columns). As such, for your case, as you have an array, you will want to duplicate this N times vertically and so we set the first parameter to N. The second parameter, the columns stay the same so we specify this to be 1 as we don't want to have any duplications... and thus the call to repmat you see above.
For more information on repmat, check out this link: http://www.mathworks.com/help/matlab/ref/repmat.html

How can I flatten every n rows in matrix using Matlab?

I can easily flatten an entire matrix into one row using reshape(M,1,[]). However, this time I want to flatten every n rows into one row. Thus, if we start with 100 rows and n=10, we will end up with 10 rows.
e.g.
1 2 3
4 5 6
7 8 9
10 11 12
with n=2 changes into
1 2 3 4 5 6
7 8 9 10 11 12
Is there a simple way to do this?
Suppose your original matrix is m, then:
reshape(m',[6 2])'
produces the required output. I'll leave it to you to generalise to other cases; comment or post again if that causes you problems.
This should work.
reshape(M',l/n,n)'
Where n is what you've defined and l is the total elements in M.
EDIT: Made it one-liner