Accessing an element of an array using an array as its index.
古いコメントを表示
Hello,
I am writing up an interpolation function and need to define the vertices of the hypercube surrounding the current values.
My current code to do this is:
% (3) Calculate the A values that are the vertices of the N-D hypercube.
a = zeros(1,2^size(CURRENT,1)-1);
curridx = zeros(1,size(CURRENT,1));
idx = zeros(1,size(CURRENT,1));
for cnt = 1:(2^size(CURRENT,1))
change = find((mod(cnt-1,2.^(0:size(CURRENT,1)-1))==0)==1);
curridx(change) = 1*curridx(change)==0;
for cnt1 = 1:size(CURRENT,1)
idx(cnt1) = lidx(cnt1)*(curridx(cnt1)==0)+uidx(cnt1)*(curridx(cnt1)==1);
end
Subs = num2cell(idx);
a(cnt) = DATA(Subs{:});
end
With the profiler showing that the lines
Subs = num2cell(idx);
a(cnt) = DATA(Subs{:});
Are 32.1% and 23.8% of the total run-time respectively. Does anybody know a more efficient method?
Thank you!
4 件のコメント
darova
2020 年 4 月 8 日
Why do you need to convert numeric to cell?
Subs = num2cell(idx);
a(cnt) = DATA(Subs{:});
Why don't just
a(cnt) = DATA(idx); % only one values inside 'idx'?
And no for loop is needed. Use element-wise operator .*
% for cnt1 = 1:size(CURRENT,1)
% idx(cnt1) = lidx(cnt1)*(curridx(cnt1)==0)+uidx(cnt1)*(curridx(cnt1)==1);
% end
idx = lidx.*(curridx==0) + uidx.*(curridx==1);
Ayden Clay
2020 年 4 月 9 日
darova
2020 年 4 月 9 日
did you try my suggestions?
Ayden Clay
2020 年 4 月 9 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!