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)
0 个评论
采纳的回答
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 个评论
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!