# Related to comparing two vectors

7 visualizaciones (últimos 30 días)
chaaru datta el 23 de Oct. de 2023
Respondida: Fabio Freschi el 23 de Oct. de 2023
Hello all, In my work I came across a situation where I have two vectors of dimension 8 X 1 each.
These vectors have all zero values except at any two rows.
My query is how to compare the non-zero positions of these two matrices in MATLAB.
Any help in this regard will be highly appreciated.
##### 2 comentariosMostrar NingunoOcultar Ninguno
NAVNEET NAYAN el 23 de Oct. de 2023
From your query, it appears that you want to find the location of non-zero values from both vectors and then compare it (correct me if I am wrong). Can you please explain what kind of comparison do you want?
Dyuman Joshi el 23 de Oct. de 2023

Iniciar sesión para comentar.

Fabio Freschi el 23 de Oct. de 2023
You can compare the two complete vectors (you also compare zeros, but who cares?)
% data
v1 = [0 0 0 4 5 0 0 0].';
v2 = [0 0 0 4 6 0 0 0].';
v3 = [0 0 0 4 5 0 0 0].';
% engine
v1eqv2 = isequal(v1,v2)
v1eqv2 = logical
0
v1eqv3 = isequal(v1,v3)
v1eqv3 = logical
1
or you can compare nonzero indices and values
% data
v1 = [0 0 0 4 5 0 0 0].';
v2 = [0 0 0 4 6 0 0 0].';
v3 = [0 0 0 4 5 0 0 0].';
% find nonzeros
[idx1,val1] = find(v1);
[idx2,val2] = find(v2);
[idx3,val3] = find(v2);
% engine
v1eqv2 = isequal(idx1,idx2) && isequal(val1,val2)
v1eqv2 = logical
1
v1eqv3 = isequal(idx1,idx3) && isequal(val1,val3)
v1eqv3 = logical
1
If you only want to check if the nonzero positions are the same
% data
v1 = [0 0 0 4 5 0 0 0].';
v2 = [0 0 0 4 6 0 0 0].';
% find nonzeros
idx1 = find(v1);
idx2 = find(v2);
% engine
v1eqv2pos = isequal(idx1,idx2)
v1eqv2pos = logical
1
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre WLAN Toolbox en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by