필터 지우기
필터 지우기

How to call index of vector from matrix?

조회 수: 3 (최근 30일)
ha ha
ha ha 2018년 3월 31일
댓글: ha ha 2018년 4월 3일
Hello, I have: n-by-3 matrix A, where xi,yi,zi are non-integer(e.g., x=-1.23;y=2.45;z=123.11):
A contain 10million element
A=[ x1 y1 z1 % element 1
x2 y2 z2 % element 2
x3 y3 z3 % element 3
x4 y4 z4 % element 4
.......
xn yn zn] % element n , where n=10,000,000
B=[ x3 y3 z3 % B contain 2 elements
x70 y70 z70]
Question: How to call index of matrix B from matrix A?(i don't wanna use "find(ismember(A,B,'rows'));" , since time consuming with large matrix A & B)
result=[3;70]
  댓글 수: 1
Image Analyst
Image Analyst 2018년 4월 2일
I don't know what "call index of matrix" or "call index of vector" means. Perhaps you can get a native English speaker to look it over. And what would you want? Would you want a 2-by-1 vector of rows where the numbers are found? Like output = [3; 70]? Or what??? Or do you want null/empty because row 3 followed by row 70 never ever appear in A?

댓글을 달려면 로그인하십시오.

답변 (1개)

Rik
Rik 2018년 3월 31일
The code below executes in 0.35 seconds. This will scale about linearly with the number of rows in B.
%Generate data
A=rand(10000000,3);
B=A([3 70],:);
tic
%loop through B to find row indices
result=NaN(size(B,1),1);
for n=1:length(result)
true_row=...
A(:,1)==B(n,1) & ...
A(:,2)==B(n,2) & ...
A(:,3)==B(n,3);
row_idx=find(true_row);
if numel(row_idx)==0
%row not found, keep NaN
elseif numel(row_idx)>1
%more than 1 row found, throw warning
warning('no unique match for row')
%you can use the line below as well, if you just want to use the
%first row that matches.
%result(n)=row_idx(1);
else
result(n)=row_idx;
end
end
toc
  댓글 수: 3
Rik
Rik 2018년 4월 2일
Comparing many elements just is very time consuming. Use the profiler or tic and toc to figure out which method is fastest for you application. I don't think there are many ways to improve beyond ismember or the method I outline here. You might check to see if arrayfun or parfor yield any speedup.
ha ha
ha ha 2018년 4월 3일
@Rik Wisselink. Thank so much

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by