Retrieving numbers present within an array by knowing the coordinates

1 vue (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 14 Fév 2023
Modifié(e) : Stephen23 le 14 Fév 2023
Hi! I have a fairly easy question but whose error I can't figure out.
I have the array "image_DEF_number_pixel" and the coordinates (x,y) in "union". I would like to create a vector containing the values present in "image_DEF_number_pixels" based on the coordinate (x,y) present in "union".
For example:
--> line 129 of "union" has coordinates (202, 188)
--> the value I want to get in a new vector is 186 because at coordinate (202, 188) inside "image_DEF_number_pixel" there is number 186.
Here is the line of code I am using:
image_DEF_numero_pixel = importdata('image_DEF_numero_pixel.mat');
union = importdata('union.mat');
vector = image_DEF_numero_pixel(union);

Réponse acceptée

Stephen23
Stephen23 le 14 Fév 2023
Modifié(e) : Stephen23 le 14 Fév 2023
The simple and efficient MATLAB approach is to use SUB2IND:
SI = load('image_DEF_numero_pixel.mat')
SI = struct with fields:
image_DEF_numero_pixel: [512×512 double]
im = SI.image_DEF_numero_pixel;
SU = load('union.mat')
SU = struct with fields:
union: [946×2 double]
un = SU.union;
idx = sub2ind(size(im),un(:,1),un(:,2));
out = im(idx)
out = 946×1
188 194 182 199 198 198 191 192 191 178

Plus de réponses (1)

Torsten
Torsten le 14 Fév 2023
image_DEF_numero_pixel = importdata('image_DEF_numero_pixel.mat')
union = importdata('union.mat')
vector = arrayfun(@(x,y)image_DEF_numero_pixel(x,y),union(1:size(union,1),1),union(1:size(union,1),2))

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by