Finding the difference between one idex and the remaining indices

1 次查看(过去 30 天)
I have a vector:
A = [1 2 3 4 5];
I want to find the difference between A(1) and the remaining indices:
A(1) = 1; 1 - A = [0 -1 -2 -3 -4]
I then want to continue to A(2) and until the end of the vector. So that I have the differenced between all points from each other.
At the moment I use loops but it is very time consuming. How can I do this using vectorization techniques to improve performance?
Many thanks! MATLAB 2016a

采纳的回答

Michael
Michael 2018-1-9
If you're using 2016a or earlier, you'll want to use bsxfun:
>> A = [1 2 3 4 5]; >> bsxfun(@minus, A.', A) ans =
0 -1 -2 -3 -4
1 0 -1 -2 -3
2 1 0 -1 -2
3 2 1 0 -1
4 3 2 1 0
Starting in 2016b (or in Octave), you can take advantage of implicit expansion and do away with bsxfun:
>> A.' - A ans =
0 -1 -2 -3 -4
1 0 -1 -2 -3
2 1 0 -1 -2
3 2 1 0 -1
4 3 2 1 0

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by