This question already has answers here:
Run-length decoding in MATLAB
(4 answers)
Closed 6 years ago.
I have a variable distr=[0 3 1 0 2];, and I have a variable full which should contrain distr(i) times i, for all i.
In this example, i want:
full=[2 2 2 3 5 5];
because distr(2)=3, therefore 3x 2, and so on.
Of course I can do it in a for-loop:
full=zeros([1,sum(distr)]);
cc=1;
for i=1:length(distr)
curr=distr(i);
full(cc:cc+curr-1)=i*ones([1,curr]);
cc=cc+curr;
end
but that is very slow. Do you know of a fast way, using MATLAB's awesome array-oriented style? Thanks!
Not sure, but maybe this will work. I can't check it since I currently don't have MATLAB:
full_tmp = arrayfun(#(i,n) i*ones(1,n),1:length(distr),distr,'uniformoutput',false);
full = cat(2,full_tmp{:});
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:
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:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Understanding why I can't use index on the result of the size function on a matrix in matlab
(1 answer)
Closed 8 years ago.
Doesn't Matlab allow chaining of matrix operations with indexing?
For eg.:
a = [1 2; 3 4];
exp(a)(:)
throws the error
Error: ()-indexing must appear last in an index expression.
It seems like this is something I would have expected Matlab to have or is there a different way to do this?
You don't need the indexing colon. The below should work.
a = [1 2; 3 4];
exp(a);
This question already has an answer here:
matlab based program where output comprises of sum of rows of input
(1 answer)
Closed 8 years ago.
I am trying to implement a vectorised solution in matlab for adding all elements above the current element in a vector. For eg.
I have a vector a as follows
a =
1
2
3
4
I would like a vector b like
b =
1
3
6
10
I know this can be done very easily using a loop but I was wondering if there are indexing options which can allow me to do the same in matlab/ octave?
You can use the Cumulative Summation function (cumsum):
b = cumsum(a)