how to velocize it (vectorizing)

a=magic(10)
b=[4;5;9;2;3;4;2;7;4;9] %width(a) element
x=ones(size(a));
for i=1:numel(b)
x(1:max(b(i)-1,1),i)=0
end

Answers (1)

I don't know why a is matter beside that the first dimension is 10
a=magic(10);
b=[4;5;9;2;3;4;2;7;4;9]; %width(a) element
h = size(a,1);
x = double(ndgrid(1:h,b)>=b(:)')
x = 10×10
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
;

9 Comments

aldo
aldo on 20 Aug 2023
Edited: aldo on 20 Aug 2023
i used magic(10) to build a matrix in a short time but the matrix that I will have to deal with has height<>width.. Do you think it will work the same? I used a matrix that has n<>m but the result is different
I probably set the problem wrong
this is code correct..excuse me
a=20;
b=[4;5;9;2;3;4;2;7;4;9] %width(a) element
x=ones(a,numel(b))
for i=1:numel(b)
x(1:max(b(i)-1,1),i)=0
end
The only thing my code requires is
numel(b) == width(a)
which obviously what you also have
b=[4;5;9;2;3;4;2;7;4;9] %width(a) element
a=200;%it's an example
x=ones(a,numel(b))
now you change a to be a size NOT a matrix as in your original question.
In my code I call it "h" and not "a".
How hard to be self consistent?
aldo
aldo on 20 Aug 2023
Edited: aldo on 20 Aug 2023
I redid the example with different variables.. It is often not easy to convey your problem to others because you have to transform the numerical arrays you use with simpler examples now i saw how my code differs from yours: with max(b)<h is equal.. with max(b)>=h it is different
h=10;
b=[4;5;9;2;3;4;2;7;4;9];
x=ones(h,numel(b))
for i=1:numel(b)
x(1:max(b(i)-1,1),i)=0;
end
x1=ones(h,numel(b))
%h = size(a,1);
x1 = double(ndgrid(1:h,b)>=b(:)');
isequal(x,x1)
ans =
logical
1
with
h=7;
b=[4;5;9;2;3;4;2;7;4;9];
ans =
logical
0
second problem:
i try to test speed code and i see it: ( with MATRIX 5710 x 82)
Elapsed time is 0.001608 seconds. (MY CODE)
Elapsed time is 0.003299 seconds. (Your Code)
size(x)
ans =
5710 82
ans =
" with max(b)<h is equal.. with max(b)>=h it is different"
because in that case your code grows x beyond the initial dimension of height h. Your final x will have more than h rows. Do you really want to do that?
h=7
h = 7
b=[4;5;9;2;3;4;2;7;4;9];
x=ones(h,numel(b));
for i=1:numel(b)
x(1:max(b(i)-1,1),i)=0;
end
x
x = 8×10
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
x1 = double(ndgrid(1:h,b)>=b(:)')
x1 = 7×10
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 1 1 0
isequal(x(1:h,:), x1)
ans = logical
1
As for speed I could possibly speed up, but who tells you that vectorized code must be faster?
aldo
aldo on 20 Aug 2023
Edited: aldo on 20 Aug 2023
"because in that case your code grows x beyond the initial dimension of height h. Your final x will have more than h rows. Do you really want to do that?"
you are right...it is correct to keep the original size of the matrix
"As for speed I could possibly speed up, but who tells you that vectorized code must be faster?"
I was convinced that such a simple code could save time compared to a loop Maybe because "ndgrid" is a resource intensive instruction?
I omitted to write at the beginning that the matrix "x" will be used of type Logical
Here is the timing on bigger arrays (I speed up the methods based on what you said about logical).
Yes for-loop is faster on online server (on my compter I get the opposite). The persons who claim for-loop is slow can come here and look or try to beat it.
h = 5710;
b=randi([0 h+1], 1, 82);
isequal(forloop(h, b), cmpb(h, b))
ans = logical
1
t_forloop = timeit(@()forloop(h, b), 1)
t_forloop = 1.6679e-04
t_cmpb = timeit(@()cmpb(h, b), 1)
t_cmpb = 2.5579e-04
function x = forloop(h, b)
x=true(h,numel(b));
for i=1:numel(b)
x(1:max(b(i)-1,1),i)=false;
end
end
function x = cmpb(h, b)
x = (1:h)'>=b(:)';
end
aldo
aldo on 20 Aug 2023
Edited: aldo on 20 Aug 2023
"Yes for-loop is faster on onlive server (on my compter I get the opposite)." i don't use online server but my pc "Processore AMD Ryzen 9 5950X 16-Core Processor, 3401 Mhz, 16 core, 32 processori logici"
I think is better to use loop in my pc because it work with parallel processor
I've noticed dramatic improvements when it comes to vectorizing code where there is a loop inside another
thanks for taking the time
Bruno Luong
Bruno Luong on 20 Aug 2023
Edited: Bruno Luong on 20 Aug 2023
"I've noticed dramatic improvements when it comes to vectorizing code where there is a loop inside another"
Not really. The speed depens what you do in the body of the loop(s), not loop are nested or not.
I know what I'm talking on speeding MATLAB code.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 20 Aug 2023

Edited:

on 20 Aug 2023

Community Treasure Hunt

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

Start Hunting!