Convert complex-valued sorted vector with absolute-value repetitions into a cell array

I have a very large vector (vC) of complex-valued entries (say mx1). I am sorting this vector (ascending or descending doesn't matter), and I keep track of the index of the entries as follows:
[sorted_Vc, ind_sorted_Vc]=sort(vC);
I am using the magnitude of the complex-valued entries to count the number of elements that have the same magnitude as follows:
abs_sorted_Vc=abs(sorted_Vc);
tt=unique(abs_sorted_Vc);
count=histc(abs_sorted_Vc, tt);
I need to save the sorted result in a cell array such that the rows represent the sorted unique absolute values as given by tt above. Further, in each row I would like to have a number of columns that include the complex-valued entries that correspond to the particular magnitude. I will also need to keep track of the indices of these entries given by ind_sorted_Vc above. To keep track of the indices, I can use another cell array of the same size as the desired complex-valued cell array.
I was able to program this in Matlab without using the above Matlab functions, however, since I am dealing with very big vector sizes on the order of 1 million entries or more, my program is so slow.
I thought through using the built-in Matlab functions such as those provided above, I might be able to efficiently implement it. Any ideas on how should I proceed further.
Thanks.

 采纳的回答

abs_sorted_Vc = abs(sorted_Vc);
[tt, tt_a, tt_b] = unique(abs_sorted_Vc);
Q = accumarray(tt_b(:), sorted_Vc(:), [], @(x) {length(x); x(:)});
This returns a cell array, each element of which is a column vector, the first element of which is the length of the vector, and the remaining elements are the complex values. The length does not really need to be stored as it can be reconstructed via cellfun(@length,Q) but I decided to leave it in anyhow for illustrative purposes.
The index values found in tt_b could also be stored during the accumarray operation, requiring extra complexity. For example,
Q = accumarray([tt_b(:); tt_b(:)], [tt_b(:); sorted_Vc(:)], [], @(x) {x(:)});
Then the first half of the elements in each cell would be indices and the second half would be corresponding values.

2 个评论

Thanks very much Walter. I wasn't aware of accumarray , and I understood its basic functionality thanks to your explanation. Very much appreciated. I am having another question related to this topic, but I will post it in a separate thread. Regards.
I don't know how efficient this is as the hit count gets higher... I do not know how it manages memory internally as the cells grow.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File 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