Assigning a list of numbers to their corresponding cells in a mesh
1 次查看(过去 30 天)
显示 更早的评论
Hello all,
I have the mesh as showing below. The cells index vector is right next to it.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1056240/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1056245/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1056250/image.png)
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.
0 个评论
采纳的回答
Voss
2022-7-5
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
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)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Weather and Atmospheric Science 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!