Asked by Nora Khaled
on 28 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,

Answer by Matt J
on 28 Apr 2019

Accepted Answer

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

Nora Khaled
on 28 Apr 2019

Thank you! this works!

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

Sign in to comment.

Answer by dpb
on 28 Apr 2019

Edited by 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

ADDENDUM:

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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## dpb (view profile)

Direct link to this comment:https://kr.mathworks.com/matlabcentral/answers/458989-how-to-concatenate-k-vectors-together-without-knowing-the-value-of-k#comment_699191

## Nora Khaled (view profile)

Direct link to this comment:https://kr.mathworks.com/matlabcentral/answers/458989-how-to-concatenate-k-vectors-together-without-knowing-the-value-of-k#comment_699196

Sign in to comment.