More effcient way to generate unit vector multiples (no for loop)?

2 ビュー (過去 30 日間)
Colton
Colton 2015 年 10 月 7 日
編集済み: Colton 2015 年 10 月 8 日
Hi, I am trying to generate unit vectors of N dimensions, and all the multiples of these unit vectors out to some distance, d. I want to return both the positive and negative directions. The only way I can do this so far is with the for loop below, which gets slow for large d. Order of vectors in final matrix is unimportant. Any tips would be greatly appreciated.
tic;
d=1e5;
N=25;
unit_vecs=zeros(2*N*d,N); %pre-allocation
I=speye(N);
for i=1:d
unit_vecs(((i-1)*2*N+1):(i*2*N),:)=cat(1,i*I,-i*I);
end
toc

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 10 月 7 日
unit_vecs = kron((1:d)',[I;-I]);

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by