Help with nested for loop generating coordinates

3 次查看(过去 30 天)
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?

请先登录,再进行评论。

类别

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