I have same values for all the 3 matrices

1 view (last 30 days)
Balaji Ramdas
Balaji Ramdas on 3 Nov 2021
Edited: Rik on 28 Nov 2021
All the 3 matrices should show a diffent values but i get to see all are the same with a warning stating " The variable XXXX appeaqrs to change the sign on every loop iteration"
Here is the code:
for m=1:length(y)
for n=1:length(x)
r1source(m,n)=sqrt((x(n)-x1)^2+(y(m)-y1)^2);
r2source(m,n)=sqrt((x(n)-x2)^2+(y(m)-y2)^2);
r3source(m,n)=sqrt((x(n)-x3)^2+(y(m)-y3)^2);
end
end
  1 Comment
Rik
Rik on 3 Nov 2021
Without input data I don't see why the resulting matrices would be te same, nor where the warning would be coming from.
This can probably be done without a loop anyway, and if is can't you should replace length with numel, since that is probably what you mean.

Sign in to comment.

Answers (1)

KSSV
KSSV on 3 Nov 2021
You need to initialize the variables which store data inside the for loop.
% Initilization
r1source = zeros(length(y),length(x)) ;
r2source = zeros(length(y),length(x)) ;
r3source = zeros(length(y),length(x)) ;
% loop
for m=1:length(y)
for n=1:length(x)
r1source(m,n)=sqrt((x(n)-x1)^2+(y(m)-y1)^2);
r2source(m,n)=sqrt((x(n)-x2)^2+(y(m)-y2)^2);
r3source(m,n)=sqrt((x(n)-x3)^2+(y(m)-y3)^2);
end
end
  13 Comments
KSSV
KSSV on 3 Nov 2021
Thanks is accepting/ voting the answer. :)
Balaji Ramdas
Balaji Ramdas on 28 Nov 2021
Edited: Rik on 28 Nov 2021
Just another question, how do i create the meausrement plane into imaginary ? as source plane the z axis value is zero but for the measurement plane it has to be an imaginary plane with z axis value of 0.2 from the real plane
%%source's positions
% source 1
x1=0.4;
y1=0.2;
z1=0;
%source 2
x2=0.8;
y2=0.55;
z2=0.2;
%source 3
x3=0.5;
y3=0.65;
z3=0.5;
f=1000; %Hz frequency
A1=1; %amplitude
w=2*pi*f; % angular speed
c=343; %m/s speed of sound in air
lambda=c/f; %m wave length
k0=w/c; %wave number
s=20; %matrix size
j=0.00001; % imaginary part
pref=2*10^-5; %Pa
p0=1.275; %kg/m3 air density
%aperture or Matrix size
x=linspace(1,0,s);
y=linspace(1,0,s);
z=linspace(1,0,s);
%Source plane
% Initilization
r1source = zeros(length(y),length(x),length(z)) ;
r2source = zeros(length(y),length(x),length(z)) ;
r3source = zeros(length(y),length(x),length(z)) ;
for m=1:length(y)
for n=1:length(x)
for o=1:length(z)
r1source(m,n,o)=sqrt((x(n)-x1)^2+(y(m)-y1)^2+(z(o)-z1)^2);
r2source(m,n,o)=sqrt((x(n)-x2)^2+(y(m)-y2)^2+(z(o)-z2)^2);
r3source(m,n,o)=sqrt((x(n)-x3)^2+(y(m)-y3)^2+(z(o)-z3)^2);
end
end
end
%Measurement plane
% Initilization
r1measurement = zeros(length(y),length(x),length(z)) ;
r2measurement = zeros(length(y),length(x),length(z)) ;
r3measurement = zeros(length(y),length(x),length(z)) ;
for m1=1:length(y)
for n1=1:length(x)
for o1=1:length(z)
r1measurement(m1,n1,o1)=sqrt((x(n1)-x1)^2+(y(m1)-y1)^2+(z(o1)-z1)^2);
r2measurement(m1,n1,o1)=sqrt((x(n1)-x2)^2+(y(m1)-y2)^2+(z(o1)-z2)^2);
r3measurement(m1,n1,o1)=sqrt((x(n1)-x3)^2+(y(m1)-y3)^2+(z(o1)-z3)^2);
end
end
end

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!