필터 지우기
필터 지우기

convert a double array to something like logical

조회 수: 2 (최근 30일)
freebil
freebil 2013년 10월 15일
답변: hayim 2014년 1월 13일
Hello. I want to concert every
1-> 1 0 0 0 0 0 0
2-> 0 1 0 0 0 0 0
3-> 0 0 1 0 0 0 0
4-> 0 0 0 1 0 0 0
5-> 0 0 0 0 1 0 0
6-> 0 0 0 0 0 1 0
7-> 0 0 0 0 0 0 1
example: I have a vector A=[1;5;7;4]; I want it to be S= [
1 0 0 0 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 0 1
0 0 0 1 0 0 0];
Is it possible to do this without for loop? Thanks

답변 (3개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 15일
편집: Azzi Abdelmalek 2013년 10월 15일
A=[1;5;7;4];
B=zeros(numel(A),max(A))
B(sub2ind(size(B),(1:numel(A))',A))=1

Vivek Selvam
Vivek Selvam 2013년 10월 15일
편집: Vivek Selvam 2013년 10월 21일
Hi Theodor,
You can try a look-up table like this:
lut = logical(diag(ones(1,7)));
A = [1 5 7 4]';
S = lut(A(:),:);

hayim
hayim 2014년 1월 13일
Late to the party, but how about this?
A = [1 5 7 4]';
S = bsxfun(@eq,A,1:7)
ans =
1 0 0 0 0 0 0
0 0 0 0 1 0 0
0 0 0 0 0 0 1
0 0 0 1 0 0 0
bsxfun is your friend!

카테고리

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