我有一个function test({
item1,
item2 = 0
}) {
const obj = { item1, item2 };
print(obj);
}
test({
item1: "foo"
});
function print(obj) {
console.log(obj.item1 + " " + obj.item2);
}
-D数据集,我想选择尺寸为n
的最大值的数据
n
的大小为:data_finale
,我想找到一种比下面的循环更有效的方法来获取最大为172299*11*5
的值
data_finale(ii,:,5)
答案 0 :(得分:0)
如果我做对了,您希望像XxYxZ矩阵一样根据矩阵的尺寸找到最大值,则想沿着X dim,Y dim和Z dim找到最大值。如果是这种情况,为什么不将max函数与尺寸选择性一起使用?您已经在使用max来找到最大的数字。
s = rng(8675309); % set random # gen
test = rand(13,4,6)*50; % creates a 3D matrix
[Xm, Xidx] = max(test,[],1) % Finds max across X dim
[Ym, Yidx] = max(test,[],2) % Finds max across Y dim
[Zm, Zidx] = max(test,[],3) % Finds max across Z dim
我一直在仔细阅读您提供的代码,并试图弄清楚您在做什么。代码正确吗,因为您尝试提取的内容有点困惑。
任何一种方式。
在第5个Z平面上使用max函数生成下面的idx
[~ y]= max(test(:,:,5),[],2)
y =
1
3
1
4
4
4
2
3
2
3
3
3
3
使用您的代码来挤压并找到最大的单个行会生成以下idx:
for ii=1:size(test,1)
[II JJ(ii)]=max(squeeze(test(ii,:,5))); % Tracking your IDX numbers
data_finale_opt(ii,:)=squeeze(test(ii,JJ(ii),:));
end
JJ = transpose(JJ)
JJ =
1
3
1
4
4
4
2
3
2
3
3
3
3
您的方法和我的方法均生成相同的目标IDX值。我们可以从for循环中删除max
函数,而保留数据部分的提取。下面的代码显示了我的代码,您的代码和速度。
s = rng(8675309); % seed to make same rand values
test = rand(172299,11,5);
%% My code
tic
[~,idx] = max(test(:,:,5),[],2);
data_finale_opt = zeros(size(test,1),size(test,3)); % building container before loop speeds up process as resources don't go into expanding the matrix.
for ii = 1:size(test,1)
data_finale_opt(ii,:)=squeeze(test(ii,idx(ii),:));
end
toc
%% Your code
tic
for ii=1:size(test,1)
[II JJ]=max(squeeze(test(ii,:,5)));
data_finale_opt2(ii,:)=squeeze(test(ii,JJ,:));
clear II JJ
end
toc
Time elapsed for my code: 0.807906 seconds.
Time elapsed for your code: 4.672404 seconds.