## How to concatenate k vectors together without knowing the value of k ?

Nora Khaled

### Nora Khaled (view profile)

님이 질문을 제출함. 28 Apr 2019
최근 활동 Nora Khaled

### Nora Khaled (view profile)

님이 댓글을 추가함. 30 Apr 2019
Matt J

### Matt J (view profile)

님이 답변을 채택함.
Hello !
I have an array of different size vectors S.
I want to get for example 3 of the vectors then put them all in one. after that use the unique operation.
[k1 k2 k3]=S{1,[1,3,5]};
all=[k1;k2;k3];
unique(all);
However I want to get an unknown number of vectors out of S. so I need them to be added to one vector without the using line 2.
Or Another way is to get the values out of these vector without repetition with out the need of having all the values in one vector. (I dont know any function which do that)
I know the indices I need from S by: (the number of non zero element of x)
[k1 k2]=S{1,[find(x)]};
Thank you,

dpb

### dpb (view profile)

28 Apr 2019
So, how do you know which columns of the array are of interest? Show/tell us how you do that and we can show you ways to solve the problem commensurate with that selection process.
BTW, do NOT use all as a variable; it is an important builtin logic function in MATLAB.
Nora Khaled

### Nora Khaled (view profile)

28 Apr 2019
I have a vector that will contain zeros and ones and I am going to find the indicies with the ones and use it in S.
my code is not working yet because I have to do all these operations in one line in the max function
function [] = linearSolverCVX(actions, S, K)
n = length(actions);
lowerBound=zeros(n,1);
upperBound=ones(n,1);
cvx_begin
variable x(n) k1 k2 all
[k1 k2]=S{1,[find(x)]} % find the nonzero elements of x and get the its sets
allk = [k1;k2]
maximize( length(unique(allk)) )
subject to
lowerBound <= x <= upperBound
sum(x) == K
cvx_end
end
in this case I have only 2 elements of x equal to 1. but I want it to be more general for k elements.
Also, Thank you for telling me about "all" variable !

로그인 to comment.

## 답변 수: 2

Matt J

### Matt J (view profile)

님의 답변 28 Apr 2019
채택된 답변

unique(vertcat(S{1,[1,3,5]}));

Nora Khaled

### Nora Khaled (view profile)

28 Apr 2019
Thank you! this works!
dpb

### dpb (view profile)

28 Apr 2019
As would
u=unique(K(:,S));
without the need for vertcat explicitly... :)
Nora Khaled

30 Apr 2019
Thank you

로그인 to comment.

dpb

### dpb (view profile)

님의 답변 28 Apr 2019
dpb

### dpb (view profile)

님이 편집함. 28 Apr 2019

"... I have to do all these operations in one line in the max function"
Well, no, not really -- that's a restriction you've placed on yourself. You can certainly do whatever operations you need to build the result.
But, if I understand that you simply want the unique set of elements of some specific columns in an array where the columns are specified by a logical addressing array, then that's simple enough to do generically regardless of how many elements are True in the logical vector--and, you get the single column for free...
u=unique(K(:,S));
unique will return the sorted list as a column vector from the subset array selected from K by S