i want to quantize the matrix values in terms of 0s and 1s...plz help me

1 visualización (últimos 30 días)
i've matrix of 3x3
a = [ 12 21 13; 32 45 67; 43 22 91 ]
my condition is
if:
a(i,j)<=a(i,j+1) a(i,j)=0
elseif
a(i,j)>a(i,j+1) a(i,j)=1
i've written the code as:
for i=1:length(a)
for j=1:length(a)-1
if(a(i,j)<=a(i,j+1))
a(i,j)=1;
else
a(i,j)=0;
end
end
end
a
the output is:
a = 1 0 13
1 1 67
0 1 91
now i want to retain only those columns which are in 0s and 1s i.e only column 1 and 2 i.e
a = 1 0 1 1 0 1
i know that a(:,1) gives me the only first column and a(:,2) gives me the second column.
but how to get both the columns like i said above
can anybody tell me how to do so programatically
plz help me

Respuesta aceptada

Iain
Iain el 11 de Jun. de 2013
General answer:
b = a(:,vector_of_columns_i_want_to_keep)
Specfic answers:
b = a(:,[1 2])
b = a(:,1:2)
b = a(:,1:end-1)

Más respuestas (1)

dpb
dpb el 11 de Jun. de 2013
>> a = [ 12 21 13; 32 45 67; 43 22 91 ]
a =
12 21 13
32 45 67
43 22 91
>> for i=1:size(a,2)-1,a(:,i)=a(:,i)<a(:,i+1);end
>> a
a =
1 0 13
1 1 67
0 1 91
>> bsxfun(@lt,a(:,2:3),a(:,1:2))
ans =
1 0
0 0
0 0
>>
  3 comentarios
Raman
Raman el 12 de Jun. de 2013
when i write this
bsxfun(@lt,a(:,1:end-1),a(:,2:end))
the output it produces is not correct. it gives me
0 1
0 1
1 1
instead of giving:
1 0
1 1
0 1
can u tell me how this can be done in order to correct it...
dpb
dpb el 12 de Jun. de 2013
Sorry for the screwup first time--I had both reassigned a and inadvertently reversed the order of the rows in the test. Then to compound the problem just did a cut'n paste w/o paying attention --
So, clean start--
>> a = [ 12 21 13; 32 45 67; 43 22 91 ]
a =
12 21 13
32 45 67
43 22 91
>> bsxfun(@lt,a(:,1:end-1),a(:,2:end))
ans =
1 0
1 1
0 1
This is the same as explicitly writing
>> [a(:,1)<a(:,2) a(:,2)<a(:,3)]
ans =
1 0
1 1
0 1
>>
hth...
--dpb

Iniciar sesión para comentar.

Categorías

Más información sobre Logical 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