Max of vector in higher dimensional array subject to constraints

1 次查看(过去 30 天)
Hello all,
I currently have code that's working but inefficient due to nested for loops, and I would like to vectorize it as possible. My data set is a higher dimensional array (a,b,.....z), and I'd like to find the maximum of each vector of z subject to a constraint based on the values of the other vectors.
For example, take a 3 dimensional array (a,b,c). I will have calculated a matrix (a x b-sized) of a constraint. I'd like to find a (a x b-sized) matrix that gives the maximum of each c vector that is measured only on the domain of (constraint:end). Let me know if there is a way to do this without for loops.
(edited for clarity)

采纳的回答

Matt J
Matt J 2020-2-18
编辑:Matt J 2020-2-18
N=ndims(A); %A is the given matrix
z=size(A,N);
idx=reshape(1:z, [ones(1,N-1),z] ) < constraint; %requires R2016b or higher
B=A;
B(idx)=-inf;
result=max(B,[],N);
  2 个评论
James Lee
James Lee 2020-2-19
Hey Matt,
This worked. I had to modify it a bit because I realized I had made a small mistake in the prompt (the constraint was an index to the matrix as opposed to a value) but this approach helped a lot. I appreciate it!
Matt J
Matt J 2020-2-19
My solution did assume that constraint is a matrix of indices. So, I'm not sure why you had to change anything. Glad you got what you need, though.

请先登录,再进行评论。

更多回答(1 个)

darova
darova 2020-2-18
Use max()
B = max(A,[],3);

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by