【发布时间】:2014-07-31 12:35:46
【问题描述】:
我正在编写一个函数,该函数需要将任意维度矩阵中的某些值删除到指定维度中。
例如,假设我有一个 3x3 矩阵:
a=[1,2,3;4,5,6;7,8,9];
我可能想删除每一行中的第三个元素,在这种情况下我可以这样做
a = a(:,1:2)
但是如果 a 的维度是任意的,并且要修剪的维度被定义为函数中的参数怎么办?
使用线性索引和一些经过仔细考虑的数学是一种选择,但我想知道是否有更简洁的解决方案?
对于那些感兴趣的人,这是我当前的代码:
...
% Find length in each dimension
sz = size(dat);
% Get the proportion to trim in each dimension
k = sz(d)*abs(p);
% Get the decimal part and integer parts of k
int_part = fix(k);
dec_part = abs(k - int_part);
% Sort the array
dat = sort(dat,d);
% Trim the array in dimension d
if (int_part ~=0)
switch d
case 1
dat = dat(int_part + 1 : sz(1) - int_part,:);
case 2
dat = dat(:,int_part + 1 : sz(2) - int_part);
end
end
...
【问题讨论】:
-
你不能提供一个任意尺寸的例子吗?
-
丹,这没有意义
标签: arrays matlab matrix multidimensional-array