Creating an array from a loop
2 次查看(过去 30 天)
显示 更早的评论
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)
采纳的回答
Image Analyst
2018-12-7
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!