필터 지우기
필터 지우기

Filling a vector with ones based on a given value

조회 수: 7 (최근 30일)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2020년 5월 8일
댓글: Ameer Hamza 2020년 5월 8일
HI all again,
I have one vector A and a matrix B:
Vector A has integers values from 0 to 6, A=[0 1 3 2 0 4 5 2 1 6]' (column vector, 10 values)
Matrix B has size 10 X 6, where in each row the element of A is split into ones, until the A(i) value is reached
for example B would be like that B=[0 0 0 0 0 0;1 0 0 0 0 0;1 1 1 0 0 0;....]
Is there any quick way to do it, avoiding multiple "if'?
thanks a lot,
Nikolas

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 8일
편집: Ameer Hamza 2020년 5월 8일
A = [0 1 3 2 0 4 5 2 1 6].';
M = repmat(1:6, numel(A), 1) <= A;
Result
>> M
M =
10×6 logical array
0 0 0 0 0 0
1 0 0 0 0 0
1 1 1 0 0 0
1 1 0 0 0 0
0 0 0 0 0 0
1 1 1 1 0 0
1 1 1 1 1 0
1 1 0 0 0 0
1 0 0 0 0 0
1 1 1 1 1 1
It gives a logical matrix. To get a numeric matrix
M = M*1;
  댓글 수: 2
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2020년 5월 8일
thanks a lot,
It's what I want, and it's much quicker than what I tried!
thanks!!
Ameer Hamza
Ameer Hamza 2020년 5월 8일
I am glad to be of help.

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

추가 답변 (1개)

Matt J
Matt J 2020년 5월 8일
편집: Matt J 2020년 5월 8일

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by