0

I have the following nested for loop that uses ismember to identify the locations where two arrays have the same value. I am doing this so that I can find the data associated with my smaller array tp (see below).

% xw is a 1xM array of position data
% yw is a 1xN ...
% zw is a 1xP ...
TP = combvec(xw',yw',zw')';    % you must have the deep learning toolbox to use this for the latest version of Matlab 2018b
in = inpolyhedron(f,v,TP);     % I would have done inpolyhedron(f,v,Xw,Yw,Zw) where Xw, Yw, and Zw are meshgrids from xw, yw, and zw but doing so exceeds the maximum array size in MatLab.
tp = TP(in,:);  Ltp = length(tp);
in3 = zeros(N,M,P);

for l=1:Ltp
   for ii=1:M
      for jj=1:N
         for kk=1:P
             if ismember(tp(l,:),TP,'rows')
                in3(jj,ii,kk) = ismember(tp(l,:),TP,'rows');
             end
         end
      end
   end
end

a = A(in3);     % A is a NxMxP array
% a should also be a NxMxP array with the majority of the points being zero, and the points corresponding to tp should be non-zero.

I tried applying what was described here but I'm not sure how to make it work because I am using ismember and because I'm filling a 3D array.

This works fine using a for loop but I'd like to make the process faster and I think a parfor loop is the best option.

WnGatRC456
  • 337
  • 1
  • 2
  • 12
  • I think you can readily optimize by not checking the `ismember` call inside the `if` once again (we know it's `true`). But even better: replace the `l`-loop and the `if` block with `in3(jj,ii,kk) = any(ismember(tp, TP, 'rows'));`. – Andras Deak -- Слава Україні Oct 08 '18 at 22:07
  • Come to think of it, are you sure the above works as expected? Your `ii`, `jj`, `kk` loop variables only appear on the left-hand side of the assignment. Very fishy. You could help us both by providing a [mcve] complete with dummy inputs (I suggest `xw = [1,2]; yw = [3,4,5]; zw = [6,7,8,9]` for instance). – Andras Deak -- Слава Україні Oct 08 '18 at 22:09
  • The only way to provide an accurate minimal, complete, and verifiable example is to provide the actual data for `xw`, `yw`, `zw`, `TP`, `f` and `v`. Last time I did that I was told not do so. Also, your suggestion cannot possibly work because `in3(jj,ii,kk) = any(ismember(tp, TP, 'rows'))` will produce a logical `1` for every `ii`,`jj`, and `kk`. Ideally, some should be `0` hence the `if`. Why do you think its fishy? The indices `ii`-`kk` are not related to `TP` or `tp` as far as I understand the usage of `combvec`. – WnGatRC456 Oct 08 '18 at 23:01
  • I'm somewhat skeptical that the topology of your problem can't be reduced to an example with inputs of size 2*3*4. `inpolyhedron` is almost entirely irrelevant. Anyway, my point is that if the right-hand side `ismember(tp(l,:),TP,'rows')` is independent of `ii`/`jj`/`kk` then every element of `in3` must be the same. To be specific, they're all 1 or all 0 depending on the result of `any(ismember(tp, TP, 'rows'))`. – Andras Deak -- Слава Україні Oct 08 '18 at 23:06
  • You might be right. The variables `f` and `v` correspond to a vortex that develops over a rotating wing, so it is very a complex non-convex shape. If it could be reduced as you say, I'm not sure how to do it. The variable `TP` is simply all the combinations of the input vectors `xw`-`zw`. Online I found out that the function `allcomb` does the same thing as `combvec` above using a [Cartesian Product](https://en.wikipedia.org/wiki/Cartesian_product). If I could relate the indices of `TP` (which is a nx3 array) then maybe this could be simplified. – WnGatRC456 Oct 08 '18 at 23:15
  • As I understand you don't really need `f` and `v`, merely a 1d logical mask `in` (with `Ltp` nonzero values) that will select part of your large `TP` array. – Andras Deak -- Слава Україні Oct 08 '18 at 23:22
  • I'm not quite sure what you mean by logical mask, but if I'm understanding you correctly then yes. The variables `f` and `v` just define `tp`. I can easily get `in` using `inpolyhedron` but then the result is not the shape (`jj`x`ii`x`kk`) or size (`N`x`M`x`P`) I need. – WnGatRC456 Oct 08 '18 at 23:24
  • 1
    "Logical mask" usually means an array of booleans that when used in indexing will take those values from the array where the mask is `true`. – Andras Deak -- Слава Україні Oct 08 '18 at 23:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181511/discussion-between-wngatrc456-and-andras-deak). – WnGatRC456 Oct 08 '18 at 23:27

0 Answers0