필터 지우기
필터 지우기

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 CenterFile Exchange에서 Resizing 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