我正在寻找一种将numpy数组分割成重叠块的有效方法.我知道这numpy.lib.stride_tricks.as_strided可能是要走的路,但我似乎无法绕过它在一个广义函数中的用法,该函数适用于任意形状的数组.以下是一些特定应用的例子as_strided.
这就是我想要的:
import numpy as np
from numpy.lib.stride_tricks import as_strided
def segment(arr, axis, new_len, step=1, new_axis=None):
""" Segment an array along some axis.
Parameters
----------
arr : array-like
The input array.
axis : int
The axis along which to segment.
new_len : int
The length of each segment.
step : int, default 1
The offset between the start of each segment.
new_axis : int, optional
The position where the newly created axis is to be …Run Code Online (Sandbox Code Playgroud) 我想重载size()我的一个类的函数,以便它不返回对象的大小,而是返回特定成员的大小.问题是Matlab在类构造函数中调用obj.size以确定对象数组的大小.
例如:
classdef dataClass < handle
properties
memberVar
end
methods
function obj = dataClass(mvIn)
if nargin ~= 0
if ~ismatrix(mvIn)
error('Input must be a matrix');
end
obj.memberVar = mvIn;
end
end
function sz = size(obj, varargin)
h = @(x)builtin('size', x, varargin{:});
sz = cell2mat(cellfun(h, {obj.memberVar}', 'uni', 0));
end
end
end
Run Code Online (Sandbox Code Playgroud)
不起作用,因为对象数组的大小与memberVar的大小相同
a = dataClass(ones(100))
a =
100x100 dataClass array with properties:
memberVar: [100x100 double]
Run Code Online (Sandbox Code Playgroud)
解决方法是将重载实现为
function sz = size(obj, varargin)
idx = strcmpi(varargin, 'mv');
if any(idx)
varargin = …Run Code Online (Sandbox Code Playgroud)