Build a large array from a small array in MATLAB? [duplicate] - arrays

This question already has answers here:
Octave / Matlab: Extend a vector making it repeat itself?
(3 answers)
Closed 8 years ago.
If I have a small array like a=[1 2 3 4 5], and want to build a large array from it with repeating it, like b=[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 ....1 2 3 4 5], how can I do that in simplest way and lowest calculations?

repmat is what you are looking for
n = 5
b = repmat(a,1,n)

Related

Matlab repmat into long single dismension array? [duplicate]

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.

Difference between two arrays and create a special array [duplicate]

This question already has answers here:
Insert value at a specific spot in matlab vector or matrix
(2 answers)
Closed 7 years ago.
I think it will be better to give an example to describe my problem clearly.
Input:
a=[1 2 3 4 5 6 7 8 9 10]
b=[2 5 8]
Output:
c=[1 0 2 3 0 4 5 0 6 7]
I am trying to solve this problem using MATLAB, and looking for any efficient way or MATLAB in build function to solve this problem.
I also tried to solve it, but it requires one for loop and few if-else statements inside it.
Okay. I do not know which will work better, but I got an idea and thought to share it.
a = 1:10;
b = [2 5 8];
c = ones(1,10);
c(b) = zeros(size(b));
c(c~=0) = 1:(length(a)-length(b));

Replicating each element n times while maintaining original order [duplicate]

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)

Returning five minimum integers from a vector consists of twenty integers in MATLAB [duplicate]

This question already has answers here:
How to find the index of the n smallest elements in a vector
(2 answers)
Closed 7 years ago.
I would like to return five minimum integers from a vector consists of twenty integers in MATLAB. Any help? Thanks.
Example:
X = [6 7 8 3 5 6 7 2 5 1 0 6 6 2 9 6 3 3 4 77];
How to get the five minimum values from this vector?
You can use Sort function and then take the 5 in the end of the array
sorted_x = sort(X)
5Minimum = sorted_x(15:20)

MATLAB: doubling the occurrences of a value [duplicate]

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)])

Resources