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

5 views (last 30 days)
Nora Khaled on 28 Apr 2019
Commented: Nora Khaled on 30 Apr 2019
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 on 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 on 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 !

Matt J on 28 Apr 2019
unique(vertcat(S{1,[1,3,5]}));
##### 3 CommentsShow 1 older commentHide 1 older comment
dpb on 28 Apr 2019
As would
u=unique(K(:,S));
without the need for vertcat explicitly... :)
Nora Khaled on 30 Apr 2019
Thank you

dpb on 28 Apr 2019
Edited: dpb on 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
in general, if there is a reason you need a given orientation within an operation, you can use either the directional catenation functions Matt illustrated above or reshape --
colvec=reshape(K(:,S),[],1); % turn the output array into column vector
colvec=reshape(K(:,S),1,[]); % turn the output array into row vector
You'll note the only difference is the order of the shape arguments to reshape and the use of the empty brackets as syntax for the number of elements in the first argument if unknown.
The other ML idiom you'll see often altho it takes a separate variable to apply it to is the colon operator by itself in the addressing parens--that is syntax to turn any array of any size/number of dimensions into a single column vector.
A=K(:,S); % will be 2D array in this case
A=A(:); % return A as column vector
the last syntax is often used in passing arguments or in preprocessing inside functions for cases where the orientation of the input may not be known but is critical for the processing steps to follow so just turns the input into a known shape.