MATLAB: Creating an array from a loop

for loop

I have a list idx=[1 3 5 12].Every index represents an area in separate_areas list.For example separate_areas(3)=1615.
I want to create 2 lists:
a) an AreasList [separate_areas(10),separate_areas(3),separate_areas(5),separate_areas(12)]
using a for loop through idx
b) an ErrorList in which i substract all the combinations of the elements of the created AreasList.
The length of the ErrorList will be length(AreasList)!(factorial)

Best Answer

Try this (requires the Statistics and Machine Learning Toolbox for pdist2)
% Specify selected indexes.
indexes=[1 3 5 12]
% Every index represents an area in separate_areas list.
% Create sample data.
separate_areas = randi(10000, 1, 15)
% Get subset of separate_areas
areasList = separate_areas(indexes)'
% Use pdist2 to get differences between all the areas.
xy = [areasList, ones(length(areasList), 1)]
errorList = pdist2(xy, xy)
pdist2() is the function that does all the combinations of subtractions that you want. It shows:
areasList =
6020
6541
7482
5384
xy =
6020 1
6541 1
7482 1
5384 1
errorList =
0 521 1462 636
521 0 941 1157
1462 941 0 2098
636 1157 2098 0