Compare vector to matrix of unique dimensions

1 次查看(过去 30 天)
I have a column vector (5000x1) and a matrix (5x3). I want to find the maximum index (ii = 1,2,3) for each element of the vector against each row of the matrix where the vector element is less than or equal to the indexed element. I will end up with a matrix of size (5000x5). If the vector element is greater than all elements in a row, return 3 as the index.
Example with a (6x1) vector and (5x3) matrix:
v = [1 1 2 1 2 3]'; A = [0 1 2
1 2 3
0 0 0
1 1 2
2 3 4];
Output (6x5): B = [2 1 3 1 1
2 1 3 1 1
3 2 3 3 1
2 1 3 1 1
3 2 3 3 1
3 3 3 3 2];

采纳的回答

xtremecheez
xtremecheez 2017-11-8
For anybody looking at this question in the future:
Using permute as Andrei suggested, the following gives the exact answer as desired (though it could be made more elegant).
P = v <= permute(A,[3 1 2]);
[~,out] = max(c,[],3);
out(sum(P,3)==0) = 3;

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2017-11-8
out = sum(permute(A,[3,1,2]) <= v,3);
  1 个评论
xtremecheez
xtremecheez 2017-11-8
编辑:xtremecheez 2017-11-8
I can work with that, thanks. Edit: I unaccepted your answer in favor of my solution that produced exactly what I wanted. You led me there though, hence the upvote.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by