フィルターのクリア

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

4 ビュー (過去 30 日間)
Oscar Soden
Oscar Soden 2020 年 12 月 7 日
回答済み: John D'Errico 2020 年 12 月 7 日
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!

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 7 日
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, :);

その他の回答 (2 件)

Star Strider
Star Strider 2020 年 12 月 7 日
Probably:
ExtractedRow = ElMat(n,:);
assuming I understand what you want to do.

John D'Errico
John D'Errico 2020 年 12 月 7 日
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.

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by