This question already has answers here:
Element-wise array replication in Matlab
(7 answers)
Matlab Repeat Value
(1 answer)
Repeat copies of array elements: Run-length decoding in MATLAB
(5 answers)
Closed 5 years ago.
Let's say I have this array:
1 2 3 4
and I want to create an array like this:
1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
How could I create that?
From the documentation for repelem:
repelem Replicate elements of an array.
U = repelem(V,N), where V is a vector, returns a vector of repeated
elements of V.
So what you want is:
>> repelem([1 2 3 4],4)
Related
This question already has answers here:
Use a vector as an index to a matrix
(3 answers)
Compact MATLAB matrix indexing notation
(3 answers)
Closed 4 years ago.
For example, I have a variable i and I want to set i=(2,3) such that B(i)=B(2,3)=7.
B=[1 2 3 4
5 6 7 8];
i=('2,3');
B(i)
I am kind of stuck here. Please help.
This question already has answers here:
Return Unique Element with a Tolerance
(7 answers)
Closed 5 years ago.
When multiplying matricies what is the difference when there is a dot there? Can this give different results
You can just use the built-in Matlab function uniquetol to obtain the unique values up to some tolerance in the array and ask the length of the returned array.
Example
A = [1+1e-11 2 3 4 1 2 3]; % generate an array with 4 unique values except for some tolerance
length(uniquetol(A, 1e-10)) % will return 4
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])
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
This question already has answers here:
convert into a single row matrix [duplicate]
(3 answers)
Closed 9 years ago.
In Matlab, I want to convert a column matrix to a row matrix like this:
A =
1
2
3
4
5
6
7
8
9
To this:
A = [1 2 3 4 5 6 7 8 9]
How is this done?
You want the transpose of the matrix, which in Matlab is written as A'.