필터 지우기
필터 지우기

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