Hi. I want to translate the following matlab code in pytorch. Is there a simple way to perform it?

```
idx = randperm(numberOfDbExamples,5);
for i=1:length(idx)
k(i)=find(labels==classificationAll(idx(i)));
end
```

cl=labelNames(k,1);

Hi. I want to translate the following matlab code in pytorch. Is there a simple way to perform it?

```
idx = randperm(numberOfDbExamples,5);
for i=1:length(idx)
k(i)=find(labels==classificationAll(idx(i)));
end
```

cl=labelNames(k,1);

Hi John!

Apply torch.nonzero() to the boolean expression for the elements you want.

`nonzero()`

returns the indices of those elements in pytorch format.

Best.

K. Frank

Hi Frank, thank you for the help. I tried this:

```
k[i] = torch.nonzero(labels == classificationAll[idx[i]])
```

But I get the error: TypeError: nonzero(): argument ‘input’ (position 1) must be Tensor, not bool.

Actually, labels is an array of 4 elements (so what I want from labels == classificationAll[idx[i]] is something like [0, 0, 1, 0]) , while classificationAll is a tensor. Maybe, should I convert it in an other form?

Hi John!

Please try to rewrite your matlab code using just pytorch tensors and tensor

operations.

If you have questions, please post a complete, fully-self-contained, runnable

(or at least runnable up to where it raises an error) script together with its

output and a description of what you would like it to do differently.

Best.

K. Frank

I forgot to convert an array to tensor. Thank you very much!