is there an index maneuver if index is 0?

조회 수: 7 (최근 30일)
Martin
Martin 2019년 9월 28일
댓글: Martin 2019년 9월 30일
Hello,
I am running a ismember:
[logical idx] = ismember(hund(:,1),dogs(:,2))
('hund' is often around 3,000 rows, and 'dogs' is often around 100,000 rows).
This give me an output that could be something like this
logical = idx =
1 355
1 536
1 746
0 0
1 1042
0 0
Then I need to find the second column in the 'dogs' variable for the index and inset this in the 'hund' variable as the 2. column like this:
hund(:,2) = dogs(idx,2)
The problem is, the above does not account for if the idx is zero. I get this error:
'Index in position 1 is invalid. Array indices must be positive integers or logical values.'
Does anyone know a solution to get arround of this?
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 9월 29일
Using logical as the name of a variable can interfere with using logical as the name of a class, and will confuse people.

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

채택된 답변

the cyclist
the cyclist 2019년 9월 28일
편집: the cyclist 2019년 9월 28일
hund(logical,2) = dogs(idx(logical),2)
If hund does not already have a second column, you'll need to do something like
hund = [hund, zeros(numel(hund),1)];
beforehand.
  댓글 수: 4
the cyclist
the cyclist 2019년 9월 30일
Yeah, although I hope the camel case is a strong hint that this is a variable.
I guess hundIsInDog would do the trick here.
Martin
Martin 2019년 9월 30일
Yeah I agree with you guys, the output names however was just to be as descriptive as possible. However, thanks for the follow up and solution. Take care

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by