I have same values for all the 3 matrices
Ältere Kommentare anzeigen
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 Kommentar
Rik
am 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.
Antworten (1)
KSSV
am 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 Kommentare
Balaji Ramdas
am 3 Nov. 2021
Bearbeitet: KSSV
am 3 Nov. 2021
KSSV
am 3 Nov. 2021
They will be different if (x1,x32,x3) and (y1, y2, y3) are different. Check those values.
Balaji Ramdas
am 3 Nov. 2021
Rik
am 3 Nov. 2021
Please attach your data or generate random example data. That way we can run it to reproduce your issue.
Balaji Ramdas
am 3 Nov. 2021
KSSV
am 3 Nov. 2021
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(:))]
Balaji Ramdas
am 3 Nov. 2021
KSSV
am 3 Nov. 2021
Offcourse they will be equal as you have used the same formula to calclate both.
Balaji Ramdas
am 3 Nov. 2021
KSSV
am 3 Nov. 2021
Read about pcolor.
figure(1)
pcolor(r1source)
shading interp
colorbar
figure(2)
surf(r1source)
shading interp
colorbar
Balaji Ramdas
am 3 Nov. 2021
KSSV
am 3 Nov. 2021
Thanks is accepting/ voting the answer. :)
Balaji Ramdas
am 28 Nov. 2021
Bearbeitet: Rik
am 28 Nov. 2021
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!