# [Scilab-users] Accessing elements sharing an index from a diimension en N-D arrays

5 messages
Open this post in threaded view
|

## [Scilab-users] Accessing elements sharing an index from a diimension en N-D arrays

 Dear All, I have an N-D array A (sometimes called hyperarray) with n dimensions, where n is a parameter a priori unknown since it is, for instance, an input argument of a function. So in conceptual form each element is A(k1, k2, k3, ..., kn) I'm looking for an elegant way to extract all elements with a given index over a given dimension, for instance A(:, k2, :, ..., :) This would mean to get an hyperarray made up of the k2-th column of all remaining dimensions. If extraction could be performed replacing the indexes by a list, for instance L = list(), for i=1:n    L(\$+1) = 1:size(A)(i); end L(2) = 1 Then A(L) would work, but the result is an error message "Invalid index". The only way a list is accepted as multidimensional index is that all except the first member are 1. Thanks, Federico Miyara _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Accessing elements sharing an index from a diimension en N-D arrays

 Le 20/01/2020 à 09:18, Federico Miyara a écrit : Dear All, I have an N-D array A (sometimes called hyperarray) with n dimensions, where n is a parameter a priori unknown since it is, for instance, an input argument of a function. So in conceptual form each element is A(k1, k2, k3, ..., kn) I'm looking for an elegant way to extract all elements with a given index over a given dimension, for instance A(:, k2, :, ..., :) This would mean to get an hyperarray made up of the k2-th column of all remaining dimensions. If extraction could be performed replacing the indexes by a list, for instance L = list(), for i=1:n    L(\$+1) = 1:size(A)(i); end L(2) = 1 Then A(L) would work, but the result is an error message "Invalid index". A(L(:)) should do it. _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: Accessing elements sharing an index from a diimension en N-D arrays

 Samuel, Thanks VERY much! It certainly does the job, but I don't quite understand how. For example, to retrieve the last columns of all dimensions: A = matrix(1:120, [5,4,2 3]);     L = list(); for i=1:length(size(A))     L(\$+1) = 1:size(A)(i); end L(2) = size(A)(2);     B = A(L(:)); B is exactly the desiredresult. But when I try to see what is L(:) I get --> L(:)  ans  =    1.   2.   3.   4.   5. and --> A(ans)  ans  =    1.    2.    3.    4.    5. If I try to assign L(:) to a variable I get --> u=L(:) Can not assign multiple value in a single variable So I attempt --> [u1,u2,u3,u4] = L(:) and get  u4  =    1.   2.   3.  u3  =    1.   2.  u2  =    4.  u1  =    1.   2.   3.   4.   5. However disp(L(:)) yields    1.   2.   3.    1.   2.    4.    1.   2.   3.   4.   5. I would like to understand what's going on. Regards, Federico Miyara On 20/01/2020 05:29, Samuel Gougeon wrote: Le 20/01/2020 à 09:18, Federico Miyara a écrit : Dear All, I have an N-D array A (sometimes called hyperarray) with n dimensions, where n is a parameter a priori unknown since it is, for instance, an input argument of a function. So in conceptual form each element is A(k1, k2, k3, ..., kn) I'm looking for an elegant way to extract all elements with a given index over a given dimension, for instance A(:, k2, :, ..., :) This would mean to get an hyperarray made up of the k2-th column of all remaining dimensions. If extraction could be performed replacing the indexes by a list, for instance L = list(), for i=1:n    L(\$+1) = 1:size(A)(i); end L(2) = 1 Then A(L) would work, but the result is an error message "Invalid index". A(L(:)) should do it. ```_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users ``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
 Samuel, Thank you for your very informative answer. Regards, Federico Miyara On 23/01/2020 21:31, Samuel Gougeon wrote: Le 21/01/2020 à 00:54, Federico Miyara a écrit : Samuel, Thanks VERY much! It certainly does the job, but I don't quite understand how. For example, to retrieve the last columns of all dimensions: A = matrix(1:120, [5,4,2 3]);     L = list(); for i=1:length(size(A))     L(\$+1) = 1:size(A)(i); end L(2) = size(A)(2); // or L = list(); for i = 1:ndims(A)     L(\$+1) = :; end L(2) = \$;     B = A(L(:)); B is exactly the desiredresult. But when I try to see what is L(:) I get --> L(:)  ans  =    1.   2.   3.   4.   5. Yes, it's a bit tricky: L(:) extracts all L components, but there is no LHS recipient except the invisible ans. Then, only ans is assigned, to L(1). Other L(2:\$) are ignored. .../... If I try to assign L(:) to a variable I get --> u=L(:) Can not assign multiple value in a single variable This is somewhat reported here: http://bugzilla.scilab.org/14372 So I attempt --> [u1,u2,u3,u4] = L(:) and get  u4  =    1.   2.   3.  u3  =    1.   2.  u2  =    4.  u1  =    1.   2.   3.   4.   5. This is described on https://help.scilab.org/docs/6.0.2/en_US/brackets.html ../.. I would like to understand what's going on. (a,b,c) is the smart Scilab "deal()" operator: [a, b, c] = (3, 1, -2) --> [a, b, c] = (3, "Hi", -2)  a  =   3.  b  =   Hi  c  =    -2 Regards Samuel ```_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users ``` _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users