[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Octave-bug-tracker] [bug #63460] mean should support operation on more
From: |
Rik |
Subject: |
[Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility |
Date: |
Sat, 3 Dec 2022 20:19:37 -0500 (EST) |
Follow-up Comment #9, bug #63460 (project octave):
I think there is an easier way to solve the problem of supporting a vector of
dimensions. The key is to understand that all of the dimensions selected by
VECDIM will be operated on and reduced to a single value. So, what we want to
do is place all of the data that belongs to the selected dimensions into a
single dimension DCALC and then call whatever function we want (say sum()) and
specify that it operate over that one DCALC dimension. This can be done
pretty easily in Octave by using permute/reshape/ipermute. Sample code is
shown below and attached as m-file do_vecdim.m. There are two important
aspects of this solution: 1) it is general and works on any number of
specified dimensions, 2) it only uses built-in, i.e., compiled functions so it
is quite fast compared to a for loop solution.
function y = do_vecdim (hfcn, x, vecdim)
## Input validation
## Remove dimensions larger than actual array
vecdim(vecdim > ndims (x)) = [];
if (isempty (vecdim))
y = x;
return;
endif
## Calculate permutation vector and permute
remdims = 1:ndims (x); # all dimensions
remdims(vecdim) = []; # delete ones specified by vecdim to leave
remaining
perm = [remdims, vecdim];
y = permute (x, perm);
## Reshape to put all vecdims in final dimension
szy = size (y);
nremd = numel (remdims);
sznew = [szy(1:nremd), prod(szy(nremd+1 : end))];
y = reshape (y, sznew);
## Call function handle on single, squashed dimension
y = hfcn (y, nremd+1);
## Inverse permute back to correct dimensions
y = ipermute (y, perm);
endfunction
To use the function try something like
sz = [2, 2, 3, 4];
N = prod (sz);
x = reshape (1:N, sz);
vecdim = [1,3];
do_vecdim (@sum, x, vecdim)
(file #54052)
_______________________________________________________
Additional Item Attachment:
File name: do_vecdim.m Size:0 KB
<https://file.savannah.gnu.org/file/do_vecdim.m?file_id=54052>
_______________________________________________________
Reply to this item at:
<https://savannah.gnu.org/bugs/?63460>
_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, Rik, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, John W. Eaton, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, John W. Eaton, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, anonymous, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, Rik, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, Arun Giridhar, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, John W. Eaton, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, Rik, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, Andreas Bertsatos, 2022/12/03
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility,
Rik <=
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, Nicholas Jankowski, 2022/12/06
- [Octave-bug-tracker] [bug #63460] mean should support operation on more than 2 remaining dimensions for Matlab compatibility, Andreas Bertsatos, 2022/12/07