[~,~,idx] = unique(x);
yVals=accumarray(idx,y,[],@(x){x(:).'}).';
[~,~,idx] = unique(x);
yVals=accumarray(idx,y,[],@(x){x(:).'}).';
After all, all information needed for this output is contained in the logical array, but is then kind of voluntarily dropped by Matlab.
Well, the logical array by itself doesn't have all the information. You need to tell MATLAB for example that each cell should contain row data and not column data. Otherwise, why couldn't {[2;5], [6;9]} be the result?
The closest thing giving what you are asking for would be the following, I think:
yVals=arrayfun(@(i) b(i,a(i,:)) , 1:size(b,1),'uni',0 ).'
This doesn't really avoid a for-loop however, except syntactically.
For older MATLAB version, this FEX might be useful SplitVec
c = SplitVec(sortrows([x;y]'),1,2)
When stable ordering of y is desired
c = SplitVec(sortrows([x;y]',1),1,2)
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!