From: HAN on

I've got a vectorization problem. I want to vectorize this code. Please give me a suggestions to vectorize this code.. Thank you.

---------------------
SIZE = 100;
x1 = 1; y1 = 1; z1 = 1;
x2 = 1; y2 = 2; z2 = 3;
x3 = 3; y3 = 4; z3 = 7;

idx = 1;
for x = 1:1:SIZE
for y = 1:1:SIZE
for z = 1:1:SIZE
R1(idx) = sqrt((x-x1)^2 + (y-y1)^2 + (z-z1)^2);
R2(idx) = sqrt((x-x2)^2 + (y-y2)^2 + (z-z2)^2);
R3(idx) = sqrt((x-x3)^2 + (y-y2)^2 + (z-z3)^2);
end
end
end
---------------------
From: Matt Fig on
% Data
SIZE = 100;
x1 = 1; y1 = 1; z1 = 1;
x2 = 1; y2 = 2; z2 = 3;
x3 = 3; y3 = 4; z3 = 7;


% Engine
R1 = sqrt((SIZE-x1)^2 + (SIZE-y1)^2 + (SIZE-z1)^2)
R2 = sqrt((SIZE-x2)^2 + (SIZE-y2)^2 + (SIZE-z2)^2)
R3 = sqrt((SIZE-x3)^2 + (SIZE-y3)^2 + (SIZE-z3)^2)




You do realize that the FOR loops you have written are doing absolutely nothing but delaying the final calculation?
From: HAN on
sorry. I omitted some code..
R1, R2, R3 is matrix which it's size is SIZE * SIZE * SIZE;

SIZE = 100;
x1 = 1; y1 = 1; z1 = 1;
x2 = 1; y2 = 2; z2 = 3;
x3 = 3; y3 = 4; z3 = 7;

idx = 1;
for x = 1:1:SIZE
for y = 1:1:SIZE
for z = 1:1:SIZE
R1(idx) = sqrt((x-x1)^2 + (y-y1)^2 + (z-z1)^2);
R2(idx) = sqrt((x-x2)^2 + (y-y2)^2 + (z-z2)^2);
R3(idx) = sqrt((x-x3)^2 + (y-y2)^2 + (z-z3)^2);
idx = idx + 1; %% this code should be added.
end
end
end

"HAN " <yahoohoho(a)hanmail.net> wrote in message <hp3r7p$1sf$1(a)fred.mathworks.com>...
>
> I've got a vectorization problem. I want to vectorize this code. Please give me a suggestions to vectorize this code.. Thank you.
>
> ---------------------
> SIZE = 100;
> x1 = 1; y1 = 1; z1 = 1;
> x2 = 1; y2 = 2; z2 = 3;
> x3 = 3; y3 = 4; z3 = 7;
>
> idx = 1;
> for x = 1:1:SIZE
> for y = 1:1:SIZE
> for z = 1:1:SIZE
> R1(idx) = sqrt((x-x1)^2 + (y-y1)^2 + (z-z1)^2);
> R2(idx) = sqrt((x-x2)^2 + (y-y2)^2 + (z-z2)^2);
> R3(idx) = sqrt((x-x3)^2 + (y-y2)^2 + (z-z3)^2);
> end
> end
> end
> ---------------------