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

3 次查看(过去 30 天)
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

采纳的回答

Iain
Iain 2013-6-11
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)

更多回答(1 个)

dpb
dpb 2013-6-11
>> 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 个评论
Raman
Raman 2013-6-12
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 2013-6-12
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by