Help with nested for loop generating coordinates

조회 수: 5 (최근 30일)
Adam Fitchett
Adam Fitchett 2019년 10월 19일
댓글: Fabio Freschi 2019년 10월 19일
Hello, I need to generate a list of coordinates for electrodes in an electrode array. I figured out how to do this with multiple separate for loops, but I want to find a more elegant way to do it with nested for loops
The array has 9 shanks, and each shank has 32 electrodes. I know (or can work out in my head) the xyz coordinates of the first electrode in each shank and I know the spacing between shanks and between electrodes So I created a vector with the xyz coordinates of the first electrode in the first shank
E1=[0.00470, 0.00470, 0]
Then created a for loop to iterate for the other 31 electrodes (since each electrode is 0.00005 metres further in the z axis):
for i=2:32
E1(i,:) = E1(1,:) + [0, 0, 0.00005*(i-1)]
end
I then created a second matrix and for loop for the second shank (notice the first coordinate of the first row has advanced by 0.00030 because this is the distance in the x axis between shanks 1 and 2)
E2 = [0.00500, 0.00470, 0]
for i=2:32
E2(i,:) = E2(1,:) + [0,0,0.00005*(i-1)]
end
I do this for all 9 shanks, changing the coordinates each time, and then concatenate the 9 arrays:
E = vertcat(E1,E2,E3,E4,E5,E6,E7,E8,E9]
I appreciate that this is a very long winded and clumsy way of doing it, so how can I create a nested for loop with one index for the shank number and a second index for the electrode number, cycling through each shank and each electrode and altering the coordinates as need to produce one continuous array of coordinates?

답변 (1개)

Fabio Freschi
Fabio Freschi 2019년 10월 19일
The command you are looking for is meshgrid
% vectors with positions
x = 0.00470:.0003:.0071;
y = 0.00470;
z = 0:0.00005:.00155;
% grid generation (note tha y is fixed, and z and x are flipped)
[Z,Y,X] = meshgrid(z,y,x);
% electrodes
E = [X(:) Y(:) Z(:)]
  댓글 수: 5
Adam Fitchett
Adam Fitchett 2019년 10월 19일
편집: Adam Fitchett 2019년 10월 19일
Sure, but can I use an X and Y that vary nonlinearly i.e go up and then down and then up again?
Fabio Freschi
Fabio Freschi 2019년 10월 19일
Short answer: try!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by