How to create a matrix from given vectors
조회 수: 5 (최근 30일)
이전 댓글 표시
I have a vector A= [ 2 4 1 3 ]
How can you create a matrix which are the length of the vector values with ones. the rest zeros?
i.e I want
B= [1 1 1 1; 1 1 0 1; 0 1 0 1; 0 1 0 0]
Regards
jason
채택된 답변
Andrei Bobrov
2012년 10월 25일
C = zeros(max(A),numel(A));
C(A + (0:numel(A)-1)*size(C,1)) = 1;
B = flipud(cumsum(C(end:-1:1,:)));
추가 답변 (2개)
Azzi Abdelmalek
2012년 10월 25일
편집: Azzi Abdelmalek
2012년 10월 25일
A= [ 2 4 1 3 ];
n=length(A);
s=meshgrid(1:n);
out=cell2mat(arrayfun(@(x,y) y<=A(x),s,s','un',0))
댓글 수: 5
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!