Logical Indexing returns error

Hi,
I'm trying to extract a two column vector from another two column vector using a logical statement.
For example, if column one contains x values and column two the corresponding y values, I want to create another matrix that contains only the x and y values when x is equal to a particular number. Here's the code I've used:
extracted_data=initial_data(initial_data==0,:);
This seems to work for some small test matrices. However, when I try to scale it up to extracting data from a vector with 64516 rows, I get the following error:
??? Index exceeds matrix dimensions.
Any ideas how I can avoid this? I've achieved the result I want using a loop and "if" statements, but this is SLOW!
Many thanks in advance,
T

 채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 8월 11일

1 개 추천

It is not the number of rows. Your code won't work for nx2 array. You need to do this:
initial_data=rand(10,2);
extracted_data=initial_data(initial_data(:,1)<0.5,:)
Also, pay attention to "==0" if your data is floating point.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

T B
2011년 8월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by