필터 지우기
필터 지우기

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