MATLAB Answers

Trying to find row number in a large matrix

조회 수: 8(최근 30일)
Hi all, I am writing a code that need to be able to find the particular row number in a large nx3 matrix (where n will eventually be >100000). I created a line of code to accomplish this task, but above n=20000 it does not work. Each row is unique (the matrix will never repeat a line).
row_num = max(find(ismember(pts,[X Y Z],'rows')) %[X Y Z] is defined with a loop
Does anyone know of a way to run this where it will function at a higher n value.
edit: pts is the name of the nx3 matrix
edit: ismember is causing the problem (it is acting like it has a size limit)

  댓글 수: 3

Tommy
Tommy 25 Jun 2020
I don't have any problem with the following code, although I realize there may be more differences between my code and yours than meet the eye:
pts = rand(1000000,3);
target = randi(1000000);
row = pts(target,:);
row_num = find(ismember(pts,row,'rows'),1,'last');
assert(row_num == target)
Is it possible you should be using ismembertol?
per isakson
per isakson 25 Jun 2020
I fail to reproduce the propblem you report on my R2018b/Win10!
"(it is acting like it has a size limit)" what exactly does that mean? Full error meassage please!
Will Schneider
Will Schneider 25 Jun 2020
I believe that my issue was caused by user error. Sorry.

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

채택된 답변

per isakson
per isakson 25 Jun 2020
편집: per isakson 26 Jun 2020
Your one-liner is missing a closing parentheses and Matlab proposes an improvement
rigth-clicking "find" opens a context menu and Matlab offers to do the change
Add the ")" and your code works fine on R2018b/Win10
%%
pts = randi( 1e6, 1e6,3 ); % whole numbers
%%
x = pts( 5e5, 1 );
y = pts( 5e5, 2 );
z = pts( 5e5, 3 );
row_num = find( ismember( pts, [x,y,z], 'rows' ), 1,'last' )
%%
z = pts( 5e5, 3 ) + 17;
row_num = find( ismember( pts, [x,y,z], 'rows' ), 1,'last' )
%%
z = pts( 5e5, 3 );
row_num = max(find(ismember(pts,[x,y,z],'rows')) )
outputs
row_num =
500000
row_num =
0×1 empty double column vector
row_num =
500000
>>

  댓글 수: 0

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

추가 답변(0개)


Translated by