convert negatives matrix values to positive to then be used in mirroring for a plot on a graph
조회 수: 2 (최근 30일)
이전 댓글 표시
I will start at I am modelling a flea response by an animal moving away from the source (in this case the origin at (0,0)). I have had some difficulty when the x value coordinate of the animal position is negative, and I think this is due to the gradient between the source and the position of the animal, it works perfectly fine when the coordinates are positive or when we have a positive x value and negative y value as the graph produced is a line graph moving directly away from the origin, whereas when the x value is negative it goes towards the origin then moves away. How ive done this model is to randomly generate 5 different positions/ coordinates as a matrix and separate the x and y values. My thought process next was to convert any negative x value coordinate to positive, produce the graph and then mirror the lines that had a negative x value earlier through the y axis and this should hopefully produce the graph i want showcasing a flea response from the animal moving away from the source.
The questions sets in, how do I convert the selected negative x values from the matrix named x1, produce the graph, and then mirror those lines only that had been converted. I have attached my code so far below including how i have calculated the new positions (as this is something i need) and hope someone can help with this.
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
SL = 220; %Source Level dB re 1uPa @ 1m (made up need to find example)
a = 0.25; %Attenuation (made up need to find example)
IL = 180; %Impact Level (made up need to find example)
v = 2; %harbor porpoises swim between 0.5 - 4.2 m/s
t = 5; %seconds
s = v*t; %metres
z1 = [randi([-10 10],1,5); randi([-10 10],1,5)];
z1 = z1';
x1 = z1(1:5,1); %this shows the first 5 numbers in column 1
y1 = z1(1:5,2); %this shows the first 5 numbers in column 2
d = sqrt((x1-x0).^2+(y1-y0).^2); %Pythagoras Theorem for distance between the 2 (Source and Reciever) points
m = (y1-y0)./(x1-x0); %Gradient between the points
r1 = d;
z2 = [x1,y1] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m]; %New coordinates for new positions 5s later at a speed of 1.5m/s
x2 = z2(1:5,1);
y2 = z2(1:5,2);
r2 = d + s;
z3 = [x2,y2] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x3 = z3(1:5,1);
y3 = z3(1:5,2);
r3 = d + 2*s;
z4 = [x3,y3] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x4 = z4(1:5,1);
y4 = z4(1:5,2);
r4 = d + 3*s;
z5 = [x4,y4] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x5 = z5(1:5,1);
y5 = z5(1:5,2);
r5 = d + 4*s;
X1 = [x1 x2 x3 x4 x5];
Y1 = [y1 y2 y3 y4 y5];
X1 = X1';
Y1 = Y1';
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
댓글 수: 2
Jon
2022년 7월 7일
It is difficult from your description to understand what you are exactly trying to achieve. From what I can understand, I think that it is likely that your general approach needs to be improved, rather than finding a workaround by "mirroring" negative values.
Please provide a simple explanation of what it is you are trying to model, and a mathematical description of the flee response that you are trying to model.
One other clarification, I think you mean flee response, not flea response. A flea is a tiny jumping insect that bites cats and dogs, probably not what you are trying to model.
채택된 답변
Kevin Holly
2022년 7월 7일
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
SL = 220; %Source Level dB re 1uPa @ 1m (made up need to find example)
a = 0.25; %Attenuation (made up need to find example)
IL = 180; %Impact Level (made up need to find example)
v = 2; %harbor porpoises swim between 0.5 - 4.2 m/s
t = 5; %seconds
s = v*t; %metres
z1 = [randi([-10 10],1,5); randi([-10 10],1,5)];
z1 = z1';
x1 = z1(1:5,1); %this shows the first 5 numbers in column 1
y1 = z1(1:5,2); %this shows the first 5 numbers in column 2
negative_values = x1<0;
x1(negative_values) = -1*x1(negative_values)
d = sqrt((x1-x0).^2+(y1-y0).^2); %Pythagoras Theorem for distance between the 2 (Source and Reciever) points
m = (y1-y0)./(x1-x0); %Gradient between the points
r1 = d;
z2 = [x1,y1] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m]; %New coordinates for new positions 5s later at a speed of 1.5m/s
x2 = z2(1:5,1);
y2 = z2(1:5,2);
r2 = d + s;
z3 = [x2,y2] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x3 = z3(1:5,1);
y3 = z3(1:5,2);
r3 = d + 2*s;
z4 = [x3,y3] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x4 = z4(1:5,1);
y4 = z4(1:5,2);
r4 = d + 3*s;
z5 = [x4,y4] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x5 = z5(1:5,1);
y5 = z5(1:5,2);
r5 = d + 4*s;
X1 = [x1 x2 x3 x4 x5];
Y1 = [y1 y2 y3 y4 y5];
X1 = X1';
Y1 = Y1';
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
Mirroring entire line if x1 is negative:
X1(:,negative_values) = -1*X1(:,negative_values);
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
댓글 수: 4
Kevin Holly
2022년 7월 7일
x0 = 0;
y0 = 0;
Sr = [x0 y0]; %Source coordinates
SL = 220; %Source Level dB re 1uPa @ 1m (made up need to find example)
a = 0.25; %Attenuation (made up need to find example)
IL = 180; %Impact Level (made up need to find example)
v = 2; %harbor porpoises swim between 0.5 - 4.2 m/s
t = 5; %seconds
s = v*t; %metres
z1 = [randi([-10 10],1,5); randi([-10 10],1,5)];
z1 = z1';
x1 = z1(1:5,1); %this shows the first 5 numbers in column 1
y1 = z1(1:5,2); %this shows the first 5 numbers in column 2
negative_values = x1<0;
x1(negative_values) = -1*x1(negative_values);
d = sqrt((x1-x0).^2+(y1-y0).^2); %Pythagoras Theorem for distance between the 2 (Source and Reciever) points
m = (y1-y0)./(x1-x0); %Gradient between the points
r1 = d;
z2 = [x1,y1] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m]; %New coordinates for new positions 5s later at a speed of 1.5m/s
x2 = z2(1:5,1);
y2 = z2(1:5,2);
r2 = d + s;
z3 = [x2,y2] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x3 = z3(1:5,1);
y3 = z3(1:5,2);
r3 = d + 2*s;
z4 = [x3,y3] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x4 = z4(1:5,1);
y4 = z4(1:5,2);
r4 = d + 3*s;
z5 = [x4,y4] + diag(s./sqrt(1+m.^2))*[ones(size(x1)),m];
x5 = z5(1:5,1);
y5 = z5(1:5,2);
r5 = d + 4*s;
X1 = [x1 x2 x3 x4 x5];
Y1 = [y1 y2 y3 y4 y5];
X1 = X1';
Y1 = Y1';
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
Mirroring entire line if x1 is negative:
X1(:,negative_values) = -1*X1(:,negative_values);
figure
hold on;
plot(X1,Y1, '-*')
title('Flea Response')
xlabel('distance (m)')
ylabel('distance (m)')
Change Axes location to origin:
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Signal Processing Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







