我想生成一个n维阵列的索引ndgrid。由于尺寸可能会发生变化,是否有办法包装ndgrid,以便ndgrid的输出数量是动态的?说,例如,我想对于2维阵列的输出为:在Matlab中的ndgrid的动态输出
[output{1} output{2}]=ndgrid(1:5)
和对于3维阵列的输出为:
[output{1} output{2} output{3}]=ndgrid(1:5)
等等等等...
我想生成一个n维阵列的索引ndgrid。由于尺寸可能会发生变化,是否有办法包装ndgrid,以便ndgrid的输出数量是动态的?说,例如,我想对于2维阵列的输出为:在Matlab中的ndgrid的动态输出
[output{1} output{2}]=ndgrid(1:5)
和对于3维阵列的输出为:
[output{1} output{2} output{3}]=ndgrid(1:5)
等等等等...
如果您需要不同尺寸的不同尺寸,您可能需要考虑如下几点: creating adjacency matrix。 相关部分是:
ndim = numel(sz);
I=cell(ndim,1);
% construct the neighborhood
for di=1:ndim
I{di}=1:sz(di);
end
[I{1:ndim}]=ndgrid(I{:});
你总是可以做到这一点使用eval,创造像“[输出{1} ...输出{2}]”在一个循环的字符串。但我想有一个更清晰的方法。 – Dan
'[output {1:n}] = ndgrid(1:5)'?'? –
@ H.Muster,谢谢! – teng