필터 지우기
필터 지우기

create a matrix with numbers from vector

조회 수: 1 (최근 30일)
Elysi Cochin
Elysi Cochin 2019년 2월 13일
댓글: madhan ravi 2019년 2월 13일
i have a vector as
v = [ 1 1 1 2 2 2 3 3 4]
i wanted to create a new matrix as
M = [
1 1 1 0 0 0 0 0 0;
0 0 0 1 1 1 0 0 0;
0 0 0 0 0 0 1 1 0;
0 0 0 0 0 0 0 0 1];
how to do it?

채택된 답변

madhan ravi
madhan ravi 2019년 2월 13일
v = [1 1 1 2 2 2 3 3 4];
u=unique(v);
R=arrayfun(@(x)v==u(x),1:numel(u),'un',0);
M=+vertcat(R{:})
Gives:
M =
1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 1

추가 답변 (2개)

madhan ravi
madhan ravi 2019년 2월 13일
Simpler:
M = +(v==unique(v).')
  댓글 수: 2
Stephen23
Stephen23 2019년 2월 13일
+1 very tidy. I like that.
madhan ravi
madhan ravi 2019년 2월 13일
Thank you!

댓글을 달려면 로그인하십시오.


KSSV
KSSV 2019년 2월 13일
N = zeros(3,3,3) ;
for i = 1:3
N(i,:,i) = 1 ;
end
M = reshape(N,3,[])
  댓글 수: 2
KSSV
KSSV 2019년 2월 13일
Give Example....by the way what is use of v here?
KSSV
KSSV 2019년 2월 13일
v = [ 1 1 1 2 2 2 3 3 4] ;
v = reshape(v,[],3)' ;
N = zeros(3,3,3) ;
for i = 1:3
N(i,:,i) = v(i,:) ;
end
M = reshape(N,3,[])

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by