This question already has answers here:
Octave / Matlab: Extend a vector making it repeat itself?
(3 answers)
Closed 6 years ago.
I'm trying to take:
a = [1 2 3]
and repeat it 5 times to get:
b = [1 2 3 1 2 3 1 2 3 1 2 3 1 2 3]
but when I try:
b = repmat(a, 5, 1)
instead I get:
b =
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
I could probably do it with a for loop but I'd like to do it correctly if possible. Any suggestions? Thanks in advance
Use the following code:
b = repmat(a,1,5)
The numbers '1' and '5' refer to the amount of rows and columns that you want to repeat the matrix a. The order is important.
Related
For example, I have a matrix A (Figure 1). When the variable n = 2, I want it to be transformed to the matrix B. The red rectangle shows the transformation rule of every column. According to this rule, when the n = 3, it can become the matrix C.
I have written a script using a for loop method, but it is a waste of time when the matrix A is very large (e.g. 11688* 140000). Is there an efficient way to solve this problem?
Figure 1:
Here is a way using reshape and implicit expansion:
result = reshape(A((1:size(A,1)-n+1) + (0:n-1).', :), n, []);
For example assume that n = 3. Implicit expansion is used to extract indices of rows:
row_ind = (1:size(A,1)-n+1) + (0:n-1).';
The following matrix is created:
1 2
2 3
3 4
Extract the desired rows of A:
A_expanded = A(row_ind, :)
When the matrix row_ind is used as an index it behaves like a vector:
1
2
1 2 3
2 3 -> 2
3 4 3
4
A_expanded =
3 5 7
6 8 9
2 6 3
6 8 9
2 6 3
1 2 1
Now A_expanded can be reshaped to the desired size:
result = reshape(A_expanded, n, []);
>>result =
3 6 5 8 7 9
6 2 8 6 9 3
2 1 6 2 3 1
If you have the Image Processing Toolbox you can use im2col as follows:
result = im2col(A, [n 1], 'sliding');
This question already has answers here:
A similar function to R's rep in Matlab [duplicate]
(4 answers)
Closed 7 years ago.
A = [1 4 5 2 1 2]
How could I concisely replicate each element n times whilst maintaining the overall order e.g. if n = 3, the desired result would be:
[1 1 1 4 4 4 5 5 5 2 2 2 1 1 1 2 2 2]
For Matlab R2015a or higher use repelem
n = 3;
u = repelem(A,n)
For older versions use bsxfun
n = 3;
u = bsxfun(#mtimes ,A(:).',ones(n,1))
u = u(:)
You could do the following:
reshape(repmat(a',[3 1]),[],1)
This question already has an answer here:
MATLAB: Duplicate each element of a vector? [closed]
(1 answer)
Closed 8 years ago.
It's hard to explain so I will show an example of what I would like to do:
x = [1 2 3 4 5]
I would like the outcome to be:
x = [1 1 2 2 3 3 4 4 5 5]
Preferably without the use of a for loop, but either method would be appreciative.
Thanks.
You can also use the Kronecker tensor product (kron function) which is pretty neat:
x = kron(x,ones(1,2))
x =
1 1 2 2 3 3 4 4 5 5
If you want it sorted as you have here, you could do:
y = sort([x x]);
alternatively if the order matters:
y = reshape([x;x],[1,2*length(x)])
This question already has answers here:
Element-wise array replication in Matlab
(7 answers)
A similar function to R's rep in Matlab [duplicate]
(4 answers)
Closed 8 years ago.
Let's say, I have:
A=[1 2; 3 4];
I want to use repmat that return:
B = [1 1 2 2; 1 1 2 2; 3 3 4 4; 3 3 4 4]
Kindly need your help. Thank you
I do not know a method using repmat but here is a method using kron
kron([1 2 ; 3 4],[1 1;1 1])
ans =
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
An alternative which uses repmat is
A=[1 2; 3 4];
cell2mat(arrayfun(#(x)repmat(x,2,2),A,'UniformOutput',false))
ans =
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
arrayfun is used to evaluate each element in A using the anonymous function #(x)repmat(x,2,2) which replicates that single element into a 2x2 matrix.
The result of arrayfun is a 2x2 cell array where each element is a 2x2 matrix. We then convert this cell array into a matrix via cell2mat.
Let the data be defined as
A = [1 2; 3 4];
R = 2; %// number of repetitions of each row
C = 2; %// number of repetitions of each column. May be different from R
Two possible approaches are as follows:
The simplest method is to use indexing:
B = A(ceil(1/R:1/R:size(A,1)), ceil(1/C:1/C:size(A,2)));
If you really want to do it with repmat, you need to play with dimensions using permute and reshape: move original dimensions 1, 2 to dimensions 2, 4 (permute); do the repetition along new dimensions 1, 3 (repmat); collapse dimensions 1, 2 into one dimension and 3, 4 into another dimension (reshape):
[r c] = size(A);
B = reshape(repmat(permute(A, [3 1 4 2]), [R 1 C 1]), [r*R c*C]);
Example result for R=2, C=3 (obtained with any of the two approaches):
B =
1 1 1 2 2 2
1 1 1 2 2 2
3 3 3 4 4 4
3 3 3 4 4 4
I have got a question regarding all the combinations of matrix-rows in Matlab.
I currently have a matrix with the following structure:
1 2
1 3
1 4
2 3
2 4
3 4
Now I want to get all the possible combinations of these "pairs" without using a number twice in the same row:
1 2 3 4
1 3 2 4
1 4 2 3
And it must be possible to make it with n-"doublecolumns". Which means, when my pair-matrix goes for example until "5 6", i want to create the matrix with 3 of these doublecolumns:
1 2 3 4 5 6
1 2 3 5 4 6
1 2 3 6 4 5
1 3 2 4 5 6
1 3 2 5 4 6
....
I hope you understand what I mean :)
Any ideas how to solve this?
Thanks and best regard
Jonas
M = [1 2
1 3
1 4
2 3
2 4
3 4]; %// example data
n = floor(max(M(:))/2); %// size of tuples. Compute this way, or set manually
p = nchoosek(1:size(M,1), n).'; %'// generate all n-tuples of row indices
R = reshape(M(p,:).', n*size(M,2), []).'; %// generate result...
R = R(all(diff(sort(R.'))),:); %'//...removing combinations with repeated values