Effacer les filtres
Effacer les filtres

How do I index an entire row given a randomised value?

4 vues (au cours des 30 derniers jours)
Oscar Soden
Oscar Soden le 7 Déc 2020
I have a data set of 4 columns and 1000 rows, It is a 3d model coordinate system.
I am wondering how i can extract the entire row given the value in the frst column.
an example of the data set:
El_mat = [...., 34 0.214 0.236 0.588,
35 0.259 0.336 0.114,....]
using the randi function i have selected a random node to be examinedand i am wondering how i can write a code that extracts only that row.
n = randi([1,1000])
n = 34
I am wondering how I can write a code that extracts only that row from El_mat and then assign that row to a variable.
Thanks a mill everyone!

Réponse acceptée

Ameer Hamza
Ameer Hamza le 7 Déc 2020
You can use logical indexing
El_mat = [...., 34 0.214 0.236 0.588,
35 0.259 0.336 0.114,....]
n = 34;
idx = El_mat(:,1)==n;
output = El_mat(idx, :);

Plus de réponses (2)

Star Strider
Star Strider le 7 Déc 2020
Probably:
ExtractedRow = ElMat(n,:);
assuming I understand what you want to do.

John D'Errico
John D'Errico le 7 Déc 2020
The other answers have already suggested good ways to solve your problem. So I will comment on the problem itself.
If the first column just contains the numbers 1:1000, in that order, with EVERY integer in there, then the answer is easy. Just use an index into the indicated row. so if your array is like this, with the first column sorted...
El_mat = [(1:1000)',rand(1000,3)];
then that first column provides no additional information that you ever needed to select anything, because we know that the nth row of your array contains the integer n in the first column.
However, if the first column is not sorted, or if there are some indexes missing, then your problem cannot be solved so directly by a simple index.
El_mat = El_mat(randperm(1000),:);
Now you will need to use a tool like find or perhaps ismember to locate the corresponding row. And if there are some missing indexes in that set, then a find or ismember calll may not be sufficient, since no exact hit will then work. For example:
n = randi(1000,1)
n =
822
Now we might use a simple logical index like this:
El_mat_n = El_mat(El_mat == n,:)
El_mat_n =
822 0.45622 0.93718 0.59158
or I may have used find.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by