Indexing Multiple Vectors in MATLAB

time = [ 1 2 3 4 5 6 7 8 9 10]
xvalues = [ 1 1.2 1.5 1.6 2 2.5 2.7 4.5 5.6 5.7 .... 9.9 10] etc
When one generates multiple vectors in a loop in MATLAB say
[index,~] = find(time)
for i = index(1):index(end)
[idx(i),~] = find( xvalues>=time(i));
idx(i+1) = idx(i)xvalues(idx(i))<time(i));
end
there seems to be a problem with idx(i+1) = idx(i)xvalues(idx(i))<time(i)); but i think the problem occurs earlier in the line above that because I can't recall idx(i)
The two lines in the loop are trying to find elements in a vector
So if A = [ 1 2 3 4 5 6 7 8 9 ]
I want to obtain, say, one vector as 1 2 3, another as 4 5 6 and the other as 7 8 9
Error: ()-indexing must appear last in an index expression.

 채택된 답변

Matt J
Matt J 2013년 3월 24일
편집: Matt J 2013년 3월 24일

0 개 추천

The previous line potentially has does have a problem. In
[idx(i),~] = find( xvalues>=time(i));
you're going to get an error if find() returns empty or a non-scalar. idx(i) can only hold a scalar.
The expression
idx(i+1) = idx(i)xvalues(idx(i))<time(i))
is definitely a syntax error, but it is not clear what you want this to do, and therefore not clear how to fix it.

댓글 수: 3

T
T 2013년 3월 24일
I reedited the post.
Sounds like HISTC might be what you're looking for
[~,bins]=histc(xvalues,time)
and now you can split up the data as you wish
xvalues(bins==1)
will be all the xvalues between time(1) and time(2), etc...
If that's not what you're trying to do, I'm afraid it's still very unclear. It's also clear what form you want the final data. A cell array, perhaps?
T
T 2013년 3월 24일
That's awesome, That's what I need.

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

추가 답변 (1개)

the cyclist
the cyclist 2013년 3월 24일

0 개 추천

I am not 100% sure what you were trying to do, but
idx(i)xvalues(idx(i))
is not valid MATLAB syntax. MATLAB is confused by this.

댓글 수: 1

T
T 2013년 3월 24일
편집: T 2013년 3월 24일
With the line you mentioned, I am trying to find elements in a vector
So if A = [ 1 2 3 4 5 6 7 8 9 ]
I want to obtain, say, one vector as 1 2 3, another as 4 5 6 and the other as 7 8 9

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

카테고리

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

질문:

T
T
2013년 3월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by