I have same values for all the 3 matrices

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

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)

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

I am sorry that i was wrong in narrating the issue
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
for m1=1:length(y)
for n1=1:length(x)
r1measurement(m1,n1)=sqrt((x(n1)-x1)^2+(y(m1)-y1)^2);
r2measurement(m1,n1)=sqrt((x(n1)-x2)^2+(y(m1)-y2)^2);
r3measurement(m1,n1)=sqrt((x(n1)-x3)^2+(y(m1)-y3)^2);
end
end
for m2=1:length(y)
for n2=1:length(x)
r1construction(m2,n2)=sqrt((x(n2)-x1)^2+(y(m2)-y1)^2);
r2construction(m2,n2)=sqrt((x(n2)-x2)^2+(y(m2)-y2)^2);
r3construction(m2,n2)=sqrt((x(n2)-x3)^2+(y(m2)-y3)^2);
end
end
-------------------------------------------------------------------------------------------------------------------------------------------
What i see is r1 source, r1 measurement and r1construction has the same values,,,, similarly r2 and r3 ,,,, i suppose it has to be different
They will be different if (x1,x32,x3) and (y1, y2, y3) are different. Check those values.
They are different
x1=0.4;
y1=0.2;
%source 2
x2=0.8;
y2=0.55;
%source 3
x3=0.5;
y3=0.65;
Thank you, i can see the warning sign after initialize step has vanished,
But i still see the matrices are the same for r1 soure, r1 construction, and r3construction ,,,,, and same for r2 and r3
Please attach your data or generate random example data. That way we can run it to reproduce your issue.
here is the entire script
They are not same, they are different.
isequal(r1source,r2source)
isequal(r2source,r3source)
[min(r1source(:)) min(r2source(:)) min(r3source(:))]
[max(r1source(:)) max(r2source(:)) max(r3source(:))]
i mean the r1 source, r1 measurement, and r1 construction are the same
Offcourse they will be equal as you have used the same formula to calclate both.
Got it,,, Thank you so much,,,,, One last question,, how to generate images to indicate pressure ?
Read about pcolor.
figure(1)
pcolor(r1source)
shading interp
colorbar
figure(2)
surf(r1source)
shading interp
colorbar
Thanks is accepting/ voting the answer. :)
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.

Asked:

on 3 Nov 2021

Edited:

Rik
on 28 Nov 2021

Community Treasure Hunt

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

Start Hunting!