MATLAB Answers

is there an index maneuver if index is 0?

2 views (last 30 days)
Martin on 28 Sep 2019
Commented: Martin on 30 Sep 2019
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 Comment

Walter Roberson
Walter Roberson on 29 Sep 2019
Using logical as the name of a variable can interfere with using logical as the name of a class, and will confuse people.

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 28 Sep 2019
Edited: the cyclist on 28 Sep 2019
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)];


Show 1 older comment
Walter Roberson
Walter Roberson on 30 Sep 2019
I tend to use tf for this exact purpose, but I know it would be better to use something else, as using tf interferes with using tf() from Control System Toolbox.
I am reluctant to use names such as isDog due to long-standing tradition that names begining with is like that are used as the name of functions for testing whether the parameter is the kind of object named by the rest of the name . For example, islogical, isdouble, isinteger
the cyclist
the cyclist on 30 Sep 2019
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 on 30 Sep 2019
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

Sign in to comment.

More Answers (0)

Sign in to answer this question.