How can I extract specific rows from a data table?

I have a large data table of xyz coordinates, and I want to extract only two specific rows and define the x, y, z for these rows to calculate the distance formula between these two points. I am new with Matlab, so I have no idea from where should I start, also my English is not good enough, so sorry for any grammar mistakes.

2 Comments

Logical or direct indexing -- without some idea what rule you want to use to define the specific rows of interest, it's hard to know.
Look at a previous similar Q? <here> and see if that doesn't provide enough traction to get started...if not, describe precisely what your table structure is and what you want. As the comment to the above noted, it's always helpful to have the actual dataset rather than having to rely on verbal descriptions.
And, btw, your English is plenty good enough, your message was just a little short on details... :)

To explain my question in more details, I have similar data to this:

A = 10x3

 C   -0.7146    0.5304   -1.1481
 C   -1.1512    0.7561    0.4465
 C    0.9622    1.8652    0.1832
 C    0.2667    0.1483   -0.5122
 C   -1.0791   -2.5351   -0.7354
 H   -1.2107    1.7495   -1.6239
 H   -0.3533   -0.4565   -1.5120
 H    1.8556   -0.3747   -0.2835
 C    0.5873   -0.5978   -1.3833
 C    0.3756   -1.4333   -1.3172

A = 10×3

 C   -0.8346    0.6204   -1.0881
 C   -1.7712    0.7661    0.5565
 C    0.7822    1.0052    0.6832
 C    0.2667    0.1483   -0.5122
 C   -1.0791   -2.5351   -0.7354
 H   -1.2111    1.7295   -1.1001
 H   -0.3422   -0.4733   -1.7731
 H    1.8661   -0.3617   -0.2881
 C    0.5611   -0.5272   -1.0844
 C    0.8742   -1.9703   -1.8312

And this data is repeated 20 times with different values of xyz coordinates, so I want to pick up only the second row and the fourth row and calculate the distance formula for these two points in each table. so the result has to be 20 answers of distance.

Sign in to comment.

 Accepted Answer

% some random xyz coordinates
xyz = randn(10, 3)
xyz = 10×3
-0.7146 0.5304 -1.1481 -1.1512 0.7561 0.4465 0.9622 1.8652 0.1832 0.2667 0.1483 -0.5122 -1.0791 -2.5351 -0.7354 -1.2107 1.7495 -1.6239 -0.3533 -0.4565 -1.5120 1.8556 -0.3747 -0.2835 0.5873 -0.5978 -1.3833 0.3756 -1.4333 -1.3172
% specify two points
p1 = 2; % second point
p2 = 7; % 7th point
xyz(p1, :)
ans = 1×3
-1.1512 0.7561 0.4465
xyz(p2, :)
ans = 1×3
-0.3533 -0.4565 -1.5120
% compute the distance of two points
% p1 p2
d = sqrt(sum((xyz(p1, :)-xyz(p2, :)).^2))
d = 2.4377

3 Comments

He said he has a table variable, not a double array. Do you want to show him how to do it for a table variable?
rows = 10;
x = randn(rows, 1)
y = randn(rows, 1)
z = randn(rows, 1)
t = table(x, y, z)
Very often, the table, array and matrix are misused in question. We have to guess what the questioner asks.
Thanks a lot, this is helpful, I also have extra question related to this; how about if this data table(xyz) is repeated 20 times with different values and I need to pick up row2 and row7 each time and calculate the distance formula, what should I use? Can I use a loop and how?

Sign in to comment.

More Answers (0)

Categories

Asked:

on 27 Aug 2021

Edited:

on 28 Aug 2021

Community Treasure Hunt

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

Start Hunting!