Index an array using specific indices
조회 수: 32 (최근 30일)
이전 댓글 표시
I have a vector
x = [0,1,1,0,1,...,N] % x is vector containing either 1s or 0s of size N = 107199.
then I have another vector s, which contains index locations in x.
s= [23,100,500, 9888,....N] % s is of size 1x518
so I want to filter s to contain only indices where the values in x are 1s.
I tried something like this,
idx = x(s)~=1;
new_s = s(idx);
but it gives me this error Index exceeds the number of array elements (107520).. any idea what im doing wrongly?
댓글 수: 0
답변 (4개)
Daniel Neubauer
2022년 11월 4일
x=[1 0 1 1 0 0 0 0 1 0];
s=[9 4 6];
snew=s(x(s)==1)
댓글 수: 1
KALYAN ACHARJYA
2022년 11월 4일
편집: KALYAN ACHARJYA
2022년 11월 4일
Yes, I noticed the array length later.
DGM
2022년 11월 4일
Try
% some test vectors
x = randi([0 1],1,20)
s = randi([1 numel(x)],1,10)
% this is the content selected by s
x(s)
% and then this would be cases where s selects 1s within x
new_s = s(x(s)==1)
댓글 수: 0
dpb
2022년 11월 4일
Well, if size N = 107199 while s is of size 1x518, then 107199 > 518 so any location in x past numel(s) is going to be an out of bounds reference.
It's not clear in isolation what relationship there is (if any) between x and s so why one would expect those two to be commensurate is puzzling.
Perhaps you're looking for locations that are in s that were found in some other fashion and wanting to know if there's a corresponding matching "1" value in x at the specific location? That's about the only way I can think of at the moment to relate the two -- if that were the case then try
new_s=s(ismember(s,find(~x)));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!