What is an alternative FIND function that find indices and values of both ZERO and NONZERO elements in Matlab ?

24 visualizaciones (últimos 30 días)
Hello,
I am using Matlab and I would like to know how to find indices/values of both zero and nonzero elements of a given vector ? Can anyone help ?
For example:
X = [1 0 2; 0 1 1; 0 0 4];
How can I find indices of the elements in the first raw i.e. '1'; '0'; '2' ?
Thank you!
  3 comentarios
Gobert
Gobert el 3 de Feb. de 2016
Editada: Gobert el 3 de Feb. de 2016
@Walter, @Kirby, Thanks for your replies. See this:
E = [2, 0, 5, 9];
k = 1: numel(E);
[i,j] = find(E(k))
The output is:
i =
1 1 1
j =
1 3 4
As can be seen, the (i, j) indices for 0 element in E are not provided. How can I also obtain the (i,j) indices for any 0 valued element that can be included (together with other nonzero valued elements) in E ? That's what I want to know.
Thanks again!

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 3 de Feb. de 2016
Editada: Stephen23 el 3 de Feb. de 2016
>> E = [2, 0, 5, 9];
>> [R,C] = ind2sub(size(E),1:numel(E))
R =
1 1 1 1
C =
1 2 3 4
Or using your original example:
>> X = [1 0 2; 0 1 1; 0 0 4];
>> [R,C] = ind2sub(size(X),1:numel(X))
R =
1 2 3 1 2 3 1 2 3
C =
1 1 1 2 2 2 3 3 3
or just the first row (as your question request) is very simple:
>> C = 1:size(X,2)
C =
1 2 3
>> R = ones(size(C))
R =
1 1 1

Más respuestas (1)

Image Analyst
Image Analyst el 3 de Feb. de 2016
Try this:
X = [1 0 2; 0 1 1; 0 0 4]
[zeroRows, zeroColumns] = find(X == 0)
[nonZeroRows, nonZeroColumns] = find(X ~= 0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by