How to concatenate k vectors together without knowing the value of k ?
2 次查看(过去 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
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.
采纳的回答
更多回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!