i want to quantize the matrix values in terms of 0s and 1s...plz help me
1 次查看(过去 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
0 个评论
采纳的回答
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
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 个评论
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 Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!