Effacer les filtres
Effacer les filtres

Creating array by picking certain elements from another array

7 vues (au cours des 30 derniers jours)
Kai le 23 Oct 2018
Let's say I am given two arrays A and B of size 100x100x5. The entries of A are either NaN or integers from 1 to 10, such that the integers along the third dimension are always unique. For instance, A(50,50,:) might look like
A(50,50,:) = [1,NaN,6,2,3,NaN];
The array B contains NaN-entries at the same places as A, and random values at the other positions. So perhaps
B(50,50,:) = [21,NaN,42,-42,-21,NaN];
In my context, A is saving the component index of the points given in B. I would like to create a new array C of size 100x100x10. The third dimension should represent the component, the values in C should represent the values of the points given by B. Hence the entries of C should be defined by C(i,j,k) = B(i,j,m) if A(i,j,m) = k, and C(i,j,k) = NaN if k is not contained in A(i,j,:). In the above example, I should get
C(50,50,:) = [21,-42,-21,NaN,NaN,42,NaN,NaN,NaN,NaN];
I hope you get the idea. I am hoping to find a simpler way for solving this than some for- and if-loops. Of course it would suffice if one could create the matrix C(:,:,1), it is not a problem to run a for-loop over the components. Thanks for your help!
  2 commentaires
madhan ravi
madhan ravi le 23 Oct 2018
Give a clear example in the comments
Kai le 23 Oct 2018
Okay, I will try to be more specific. Consider the unit sphere for instance, and a regular 3-dimensional grid (-1:0.1:1)^3. The sphere intersects the grid lines parallel to the z-axis at many points, and I am trying to find "components" of these points. Now I made some algorithm that finds the "height" of the intersection and assigns a component to them. In this case of the unit sphere, I would obtain two components, representing the upper and the lower half, so I always assign the value 1 or 2 respectively.
So far these points and components are represented by the arrays A and B, which are both of size 21x21x2. The first two dimensions stand for the position of the grid line. The third dimension is some predefined "maximal sheet number". So in the case of the unit sphere I know that each line has at most two intersections with the sphere, so I can set it to 2. But for more arbitrary shapes, I do not know the exact number, so I just estimate it beforehand. Now when my "component sorting" is finished, the array A contains entries NaN, 1 and 2, and the array B saves the heights of the intersections at the respective positions. Again, in the case of the unit sphere, it is fairly simple and probably sorted already just by construction of my sorting algorithm. But in general the third dimension of A would have integer numbers in arbitrary order. What I would like to do is create a stack C of size 21x21x2, and C(:,:,1) should save the heights of the intersections on the lower half, C(:,:,2) should save the heights of the intersections on the upper half. In general, the third dimension of C should represent the component number.

Connectez-vous pour commenter.

Réponses (1)

Andrei Bobrov
Andrei Bobrov le 23 Oct 2018
s = size(A);
[ii,jj,~] = ndgrid(1:s(1),1:s(2),1:s(3));
ij = [ii(:),jj(:),A(:),B(:)];
ij = ij(~isnan(ij(:,3)),:);
out = accumarray(ij(:,1:3),ij(:,4),s + [0,0,s(3)],[],nan);


En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by