Creating an array from a loop

2 visualizaciones (últimos 30 días)
DIMITRIOS THEODOROPOULOS
DIMITRIOS THEODOROPOULOS el 7 de Dic. de 2018
Respondida: Image Analyst el 7 de Dic. de 2018
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 comentario
madhan ravi
madhan ravi el 7 de Dic. de 2018
please upload the code that you tried

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 7 de Dic. de 2018
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

Más respuestas (0)

Categorías

Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by