Assigning a list of numbers to their corresponding cells in a mesh

1 vue (au cours des 30 derniers jours)
Lama Hamadeh
Lama Hamadeh le 5 Juil 2022
Réponse apportée : Voss le 5 Juil 2022
Hello all,
I have the mesh as showing below. The cells index vector is right next to it.
If I have a vector of y values: , that has four elements. Each element corresponds to a vertical cell in the mesh where the x index of the first is 1, the second is 2, and so on. The cells that correspond to this y vector is:
I need to write a code that decide the cell for each of these elements and create a vector that contains them, as shown above. My attempt of doing it is as follwoing:
MidPoint = 4; %number of midpoints
ycells = 4; %number of cells on the y edge
Mid_y = [0.1250 0.3750 0.6250 0.8750]; %values of midpoints
dy = 0.1250; %the step
y = [0.52 0.68 0.19 0.46] %the new set of y values that need to decide their cells
for i = 1:MidPoint
for j = 1:ycells
if y(i) <= Mid_y(j)+dy && y(i) >= Mid_y(j)-dy
Cells = Cells(i*j)
end
end
end
Any help would be appreicted.
Thanks.

Réponse acceptée

Voss
Voss le 5 Juil 2022
Is something like this what you're after?
MidPoint = 4; %number of midpoints
ycells = 4; %number of cells on the y edge
Mid_y = [0.1250 0.3750 0.6250 0.8750]; %values of midpoints
dy = 0.1250; %the step
y = [0.52 0.68 0.19 0.46] %the new set of y values that need to decide their cells
y = 1×4
0.5200 0.6800 0.1900 0.4600
for i = 1:MidPoint
for j = 1:ycells
% have to decide how to handle the case that y(i) is on the edge
% between two cells; don't want to count that as belonging to
% more than one cell, so use < and >= (or <= and >):
if y(i) < Mid_y(j)+dy && y(i) >= Mid_y(j)-dy
% assign 10*i+j to element i of Cell:
Cells(i,1) = 10*i + j;
% stop looking when you find the cell that y(i) belongs to:
break
end
end
end
disp(Cells)
13 23 31 42

Plus de réponses (0)

Catégories

En savoir plus sur Weather and Atmospheric Science dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by