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!