Find differences in a set of points without a for loop
4 ビュー (過去 30 日間)
古いコメントを表示
I a vector of numbers that I am trying to find the differences between each element in the vector without a for loop. Below is a code example. Is there a more efficient way to execute this code without a for loop?
x = rand(20,1);
diff = zeros(length(x),length(x));
for ix = 1:length(x)
for iy = (ix+1):length(x)
diff(ix,iy) = abs(x(ix) - x(iy));
end
end
0 件のコメント
回答 (3 件)
Image Analyst
2015 年 4 月 21 日
編集済み: Image Analyst
2015 年 4 月 21 日
diff() is a built in function. Don't use it for a variable name.
For a microscopic length such as 20, don't worry about a for loop. It won't take any time at all, and in fact may take less than calling some fancy function.
1 件のコメント
Image Analyst
2015 年 4 月 21 日
Richard's "Answer" moved here since it is not an answer to the original question:
Thanks for the help. My actual code has a vector that is much larger. I was just using this as an example.
Mehri Mehrnia
2022 年 5 月 19 日
I have the question to find all pair-wise difference in a group without loop!
My array in scale of milions!
Bruno Luong
2022 年 5 月 19 日
編集済み: Bruno Luong
2022 年 5 月 19 日
x = randi(10,3,1)
d = abs(x - x.')
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Install Products についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!