accumarray not working! unique values
9 次查看(过去 30 天)
显示 更早的评论
pre-formatted
Owner Area ID Date&Time Loc1 Loc2 AvSpeed Loc Loc Loc Loc L M
1 1 1 734747.9197 418.2 424.9 36.53 414.2 422.5 0 0
1 1 1 734747.9273 424.9 426.3 26.77 422.5 422.5 426.3 0 0
1 1 1 734747.9295 426.3 436.5 45.03 422.5 426.3 422.5 426.3 0 0
1 1 1 734747.9389 436.5 436.5 0.027 422.5 426.3 422.5 426.3 0 0
1 1 1 734747.9669 436.5 440.7 45.23 422.5 426.3 440.7 440.7 0 0
1 1 1 734747.9707 440.7 440.7 0.007 440.7 440.7 440.7 440.7 0 0
1 1 1 734747.9787 440.7 444.2 33.46 440.7 440.7 440.7 440.7 0 0
1 1 1 734747.9831 444.2 444.2 0.004 440.7 440.7 440.7 440.7 1 8
1 1 2 734748.9002 418.2 424.9 36.53 414.2 0 422.5 0 0 0
1 1 2 734748.9079 424.9 426.3 26.77 422.5 0 422.5 426.3 0 0
What I want to do is, for each unique Owner,Area &ID, find unique values form 4 Loc columns(the total number of unique values varies for different combinations of Owner, Area & ID).
And the expected output will look sth like:
pre-formatted
Owner Area ID StartD&T StartLoc Loc1 Loc2 Loc3 Loc4 EndLoc EndD&T
1 1 1 734747.9197 418.2 422.5 426.3 440.7 444.2 734747.9831
Start loc is the smallest value in Loc1 & EndLoc is the largest value in Loc2, everything in between comes from the four Loc columns above. Usually, there will be 2-4 different of these for each unique Owner, Area, ID set. In above example, all but last two is a unique set of 1,1,1 so, I need to find the three Loc's in between from among all four Loc cloumns.
I tried accumarray, but can't get it to work. I am hoping some guidance will help me get to at least get started. Thank you so much.
2 个评论
回答(2 个)
Sean de Wolski
2012-7-3
编辑:Sean de Wolski
2012-7-3
Newest
It sounds like what you really want to do is create a matrix of yoru subs as you have and then do a unique 'rows' on this:
M = [c1(:) c2(:) c3(:) c4(:)];
[uv,~,idxM] = unique(M,'rows');
This will tell you what rows are the same (via idxM). From here you can decide how to continue and if accumarray is a good idea or if you want to merge the rows of Q, based on their idxM.
even newer
Even if you were callling it with a vector you are still going to run into the error of unique outputing more than one result. Thus you will need to wrap the results in a cell array in order to have a a scalar output.
new per comment clarification
Ahh. Yes, it means exactly what it says!
You are passing val in as an nx4 matrix. It expects a scalar or an nx1 vector. Thus you need to either call accumarray four times with each vector or not use it.
older
Since you have double precision non-integer values you will have to bin them and find their indices with either histc or unique. Here is a two-dimensional example:
x = [1.1; 1.1; 1.1; 2.2; 2.2; 4.7; 8.9; 11; 11; 8.9]; %unique this
y = rand(1,10)*5; %histc this
data = 1:10; %some data
[u,~,idxx] = unique(x); %where are the unique values in x?
[~,idxy] = histc(y,0:5); %where do the elements of y fall in 0:5
accumarray([idxx(:) idxy(:)],data)
0 个评论
Walter Roberson
2012-7-3
[a1, b1, c1] = unique( Q(:,1) );
[a2, b2, c2] = unique(Q(:,2) );
[a3, b3, c3] = unique(Q(:,3) );
tB = accumarray( repmat([c1(:), c2(:),c3(:)], 4, 1), reshape( Q(:,8:11), [], 1), [], @(V) {unique(V)}, NaN);
I am a bit uncomfortable about you wanting to apply unique() to the elements, as that is going to return a variable number of results. I have adjusted your code so that the results will be stored as a cell (needed to have accumarray have multiple values per index combination), but I do not understand from your sample output how you plan to output those unique values.
How are the empty cells in your input being represented?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!