Access to row i of a specific matrix [duplicate] - matlab

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

Related

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

MATLAB: fast creation of vector of indices [duplicate]

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{:});

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

Matlab - sum all elements above current [duplicate]

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)

Find the value in a matrix with certain condition [duplicate]

This question already has answers here:
Find vector elements matching two conditions in Matlab
(3 answers)
Closed 9 years ago.
How to find the value in a matrix with certain condition. For example,
a=[-3.14,2.12,-5,3,6,7];
b=find(a>0)
this will return the indices of matrix with that ">0" condition, which is b= 2 4 5 6.
do we have any solution for find the actual value in a matrix under that condition, such as returning b= 2.12 3 6 7 ?
you can even skip the find part:
whatyouwant = a(a>0);
That's called logical indexing in Matlab...
You could do the following
a = [-3.14,2.12,-5,3,6,7];
b = find(a>0)
c = a(b)
The c would then be the selected values based on the indices in b.
Hope it helps!