Create a generic matrix array (coordinates ) based on input data in a specific direction as attached in the photo

2 次查看(过去 30 天)
Hi guys,
I am a bit new to matlab, tyring to generate generic matrix array coordinates (x,y).. I have tried the following code, but works in the wrong direction,
So can anyone help me .... with modifying the code, or so..
thank you,
clear all;
clc;
w=1; b=1; m=2; n=2;
dx = w/m; dy = b/n;
tnod = (n+1)*(m+1); % total number of nodes
co = zeros(tnod,3);
for ii = 1:m+1
for jj = 1:n+1
node = jj + (ii-1)*(n+1);
x(ii,jj) = (ii-1)*dx;
y(ii,jj) = (jj-1)*dy;
co(node,1) = x(ii,jj);
co(node,2) = y(ii,jj); % CO not as I want
end
end
% CO result should be the following:
0 0 0
0.5 0 0
1 0 0
1 0.5 0
1 1 0
0.5 1 0
0 1 0
0 0.5 0
0.5 0.5 0

采纳的回答

Jonas
Jonas 2021-6-22
编辑:Jonas 2021-6-22
here a quite manual solution, there may be something easier
width=2.8;
height=1.5;
pointsAlongY=4;
pointsAlongX=4;
dx = width/(pointsAlongX-1);
dy = height/(pointsAlongY-1);
tnod = (pointsAlongX)*(pointsAlongY);
numberedGrid = zeros(pointsAlongY,pointsAlongX);
co=zeros(tnod,2);
currGridCoord=[1 pointsAlongY]; % x and y coordinate in grid
currRealCoord=[0 0];
currNode=1;
while 1
numberedGrid(currGridCoord(2),currGridCoord(1))=currNode;
co(currNode,:)=currRealCoord;
currNode=currNode+1;
% look if we are at the edges of the matrix or if the next entry is
% unequal 0
% we want to move right first, then up, left and down
rightCondition=currGridCoord(1)~=pointsAlongX && numberedGrid(currGridCoord(2),currGridCoord(1)+1)==0;
upCondition=currGridCoord(2)~=1 && numberedGrid(currGridCoord(2)-1,currGridCoord(1))==0;
leftCondition=currGridCoord(1)~=1 && numberedGrid(currGridCoord(2),currGridCoord(1)-1)==0;
downCondition=currGridCoord(2)~=pointsAlongY && numberedGrid(currGridCoord(2)+1,currGridCoord(1))==0;
if rightCondition && ~downCondition
currGridCoord(1)=currGridCoord(1)+1;
currRealCoord(1)=currRealCoord(1)+dx;
elseif upCondition
currGridCoord(2)=currGridCoord(2)-1;
currRealCoord(2)=currRealCoord(2)+dy;
elseif leftCondition
currGridCoord(1)=currGridCoord(1)-1;
currRealCoord(1)=currRealCoord(1)-dx;
elseif downCondition
currGridCoord(2)=currGridCoord(2)+1;
currRealCoord(2)=currRealCoord(2)-dy;
else
break;
end
end
% there you can see how we went through the matrix
numberedGrid
% list of coordinates
co

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by