how velocize it? (vectorize it)

1 view (last 30 days)
aldo
aldo on 8 Oct 2023
Edited: aldo on 8 Oct 2023
a=magic(8)
a = 8×8
64 2 3 61 60 6 7 57 9 55 54 12 13 51 50 16 17 47 46 20 21 43 42 24 40 26 27 37 36 30 31 33 32 34 35 29 28 38 39 25 41 23 22 44 45 19 18 48 49 15 14 52 53 11 10 56 8 58 59 5 4 62 63 1
b=[1 3 2 4 5 5 8 3] % (b is always >0)
b = 1×8
1 3 2 4 5 5 8 3
[~,c]=size(a);
B=a;
for i=1:c
if b(i)>1
B(1:b(i)-1,i)=0;
end
end
B
B = 8×8
64 0 0 0 0 0 0 0 9 0 54 0 0 0 0 0 17 47 46 0 0 0 0 24 40 26 27 37 0 0 0 33 32 34 35 29 28 38 0 25 41 23 22 44 45 19 0 48 49 15 14 52 53 11 0 56 8 58 59 5 4 62 63 1

Accepted Answer

Bruno Luong
Bruno Luong on 8 Oct 2023
a=magic(8)
a = 8×8
64 2 3 61 60 6 7 57 9 55 54 12 13 51 50 16 17 47 46 20 21 43 42 24 40 26 27 37 36 30 31 33 32 34 35 29 28 38 39 25 41 23 22 44 45 19 18 48 49 15 14 52 53 11 10 56 8 58 59 5 4 62 63 1
b=[1 3 2 4 5 5 8 3] % (b is always >0)
b = 1×8
1 3 2 4 5 5 8 3
B = a.* ((1:size(a,1))' >= b)
B = 8×8
64 0 0 0 0 0 0 0 9 0 54 0 0 0 0 0 17 47 46 0 0 0 0 24 40 26 27 37 0 0 0 33 32 34 35 29 28 38 0 25 41 23 22 44 45 19 0 48 49 15 14 52 53 11 0 56 8 58 59 5 4 62 63 1

More Answers (0)

Categories

Find more on Robust Control Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!