user2333346
user2333346

Reputation: 1131

Perform a find between a matrix and a vector and concatenate results - MATLAB

I have a 3D array

 a = meshgrid(2500:1000:25000,2500:1000:25000,2500:1000:25000);

Usually I use a loop to execute the following logic

k =[];
for b = 0.01:0.01:0.2
    c = find(a <= b.*0.3 & a <= b.*0.5);
    if(~isempty(c))
        for i=1:length(c)
            k = vertcat(k,a(c(i)));
        end
    end
end

How do I remove the loop? And perform the action above with one line

Of course

b = [0.01:0.01:0.2];
c=find(a<b*.8)

is not possible

Upvotes: 1

Views: 43

Answers (1)

Divakar
Divakar

Reputation: 221504

bsxfun based approach to create a mask for the finds and using it to index into a replicated version of input array, a to have the desired output -

vals = repmat(a,[1 1 1 numel(b)]); %// replicated version of input array
mask = bsxfun(@le,a,permute(b*0.3,[1 4 3 2])) & ...
    bsxfun(@le,a,permute(b*0.5,[1 4 3 2])); %// mask created
k = vals(mask); %// desired output in k

Please note that you would be needed to change the function handle used with bsxfun according to the condition you would be using.

Upvotes: 1

Related Questions