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

조회 수: 2 (최근 30일)
Nora Khaled
Nora Khaled 2019년 4월 28일
댓글: Nora Khaled 2019년 4월 30일
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,
  댓글 수: 2
dpb
dpb 2019년 4월 28일
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 2019년 4월 28일
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
Matt J 2019년 4월 28일
unique(vertcat(S{1,[1,3,5]}));
  댓글 수: 3
dpb
dpb 2019년 4월 28일
As would
u=unique(K(:,S));
without the need for vertcat explicitly... :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2019년 4월 28일
편집: dpb 2019년 4월 28일
"... 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.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by