Filling a vector with ones based on a given value

15 views (last 30 days)
Nikolas Spiliopoulos on 8 May 2020
Commented: Ameer Hamza on 8 May 2020
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 on 8 May 2020
Edited: Ameer Hamza on 8 May 2020
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;
Ameer Hamza on 8 May 2020
I am glad to be of help.

Matt J on 8 May 2020
Edited: Matt J on 8 May 2020
B=(A(:)>=1:6)