Max of vector in higher dimensional array subject to constraints
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Matt J
on 18 Feb 2020
Edited: Matt J
on 18 Feb 2020
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 Comments
Matt J
on 19 Feb 2020
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.
More Answers (1)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!