Creating an array from a loop

2 ビュー (過去 30 日間)
DIMITRIOS THEODOROPOULOS
DIMITRIOS THEODOROPOULOS 2018 年 12 月 7 日
回答済み: Image Analyst 2018 年 12 月 7 日
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)
  1 件のコメント
madhan ravi
madhan ravi 2018 年 12 月 7 日
please upload the code that you tried

サインインしてコメントする。

採用された回答

Image Analyst
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 件)

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by