Creating an array from a loop
조회 수: 4 (최근 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에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!