Setting values in a column using multiple if else statements

2 visualizzazioni (ultimi 30 giorni)
Rahul Gulia il 28 Set 2022
Risposto: Adithya il 22 Mag 2023
I hope I can describe my problem in simple words.
This is a top view of a warehouse of size 60x55 m. And I have placed shelves in the warehouse of dimension 2.5x20 m.
I have to create a column that displays the distance of each location from the nearest shelf. And this is what I have done right now.
Here, (nx,1) column refers to the x-position, and (nx,2) column refers to the y-position in the warehouse.
for nx=1:length(DatasetTmp_10)
% Adding the data for within shelf coverage (Within the shelf area, value = 0)
if ((DatasetTmp_10(nx,1)>=5 & DatasetTmp_10(nx,1)<=7.3)|(DatasetTmp_10(nx,1)>=10.3 & DatasetTmp_10(nx,1)<=12.6)...
|(DatasetTmp_10(nx,1)>=15.6 & DatasetTmp_10(nx,1)<=17.9)|(DatasetTmp_10(nx,1)>=20.9 & DatasetTmp_10(nx,1)<=23.2)...
|(DatasetTmp_10(nx,1)>=26.2 & DatasetTmp_10(nx,1)<=28.5)|(DatasetTmp_10(nx,1)>=31.5 & DatasetTmp_10(nx,1)<=33.8)...
|(DatasetTmp_10(nx,1)>=36.8 & DatasetTmp_10(nx,1)<=39.1)|(DatasetTmp_10(nx,1)>=42.1 & DatasetTmp_10(nx,1)<=44.4)...
|(DatasetTmp_10(nx,1)>=47.4 & DatasetTmp_10(nx,1)<=49.7)|(DatasetTmp_10(nx,1)>=52.7 & DatasetTmp_10(nx,1)<=55))
DatasetTmp_10(nx,14) = 0;
% In the aisle coverage (if they are inside an aisle, calculate the distance to the nearest aisle, and substitute the value)
% Here I am calculating the distance from a location from its right and left side kept shelf.
elseif ((DatasetTmp_10(nx,1)>=7.3 & DatasetTmp_10(nx,1)<=10.3))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-7.3),(10.3-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,1)>=12.6 & DatasetTmp_10(nx,1)<=15.6))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-12.6),(15.6-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,1)>=17.9 & DatasetTmp_10(nx,1)<=20.9))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-17.9),(20.9-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,1)>=23.2 & DatasetTmp_10(nx,1)<=26.2))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-23.2),(26.2-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,1)>=28.5 & DatasetTmp_10(nx,1)<=31.5))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-28.5),(31.5-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,1)>=33.8 & DatasetTmp_10(nx,1)<=36.8))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-33.8),(36.8-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,1)>=42.1 & DatasetTmp_10(nx,1)<=47.4))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-42.1),(47.4-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,1)>=49.7 & DatasetTmp_10(nx,1)<=52.7))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,1)-49.7),(52.7-DatasetTmp_10(nx,1)));
% Corner Aisles data
elseif ((DatasetTmp_10(nx,1)>=0 & DatasetTmp_10(nx,1)<=5)) % Left corner aisle
DatasetTmp_10(nx,14) = 5-DatasetTmp_10(nx,1);
elseif ((DatasetTmp_10(nx,1)>=55 & DatasetTmp_10(nx,1)<=60)) % Right corner aisle
DatasetTmp_10(nx,14) = DatasetTmp_10(nx,1)-55;
% Strong LOS cases (East-West Direction)
elseif ((DatasetTmp_10(nx,2)>=0 & DatasetTmp_10(nx,2)<=5)&&((DatasetTmp_10(nx,1)>=0 & DatasetTmp_10(nx,1)<=60)))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,2)-0),(5-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,2)>=25 & DatasetTmp_10(nx,2)<=30)&&((DatasetTmp_10(nx,1)>=0 & DatasetTmp_10(nx,1)<=60)))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,2)-25),(30-DatasetTmp_10(nx,1)));
elseif ((DatasetTmp_10(nx,2)>=50 & DatasetTmp_10(nx,2)<=55)&&((DatasetTmp_10(nx,1)>=0 & DatasetTmp_10(nx,1)<=60)))
DatasetTmp_10(nx,14) = min((DatasetTmp_10(nx,2)-50),(55-DatasetTmp_10(nx,1)));
else
DatasetTmp_10(nx,14) = NaN; % Putting Null data in here
end
end
I think I have overlapping if else conditions for multiple positions.
I would really appreciate it if someone could suggest on my code, and help me find a better solution to construct a more accurate data for my scenario.
Looking forward to any kind of suggestion.
Thank You
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Risposte (1)

It seems that the current implementation of your code involves a large number of if-else conditions to determine the distance of each location from the nearest shelf. This approach can become complex and prone to errors, especially when dealing with multiple positions and overlapping conditions.
To simplify and improve the accuracy of your code, you can consider using a distance calculation based on the Euclidean distance formula. This approach will allow you to calculate the distance between each location and the nearest shelf directly, without the need for extensive if-else conditions.
Here's an example of how you can modify your code using the Euclidean distance formula:
shelf_positions = [5, 7.3; 10.3, 12.6; 15.6, 17.9; 20.9, 23.2; 26.2, 28.5; 31.5, 33.8; 36.8, 39.1; 42.1, 44.4; 47.4, 49.7; 52.7, 55];
corner_aisle_positions = [0, 5; 55, 60];
strong_los_positions = [0, 5; 25, 30; 50, 55];
for nx = 1:length(DatasetTmp_10)
x = DatasetTmp_10(nx, 1);
y = DatasetTmp_10(nx, 2);
% Check if the location is within a shelf area
if any(x >= shelf_positions(:, 1) & x <= shelf_positions(:, 2))
DatasetTmp_10(nx, 14) = 0;
else
% Calculate the distance to the nearest shelf
distances = sqrt((x - shelf_positions(:, 1)).^2 + (y - 10).^2); % Assuming the shelves are located at y = 10
DatasetTmp_10(nx, 14) = min(distances);
% Check if the location is within a corner aisle
if any(x >= corner_aisle_positions(:, 1) & x <= corner_aisle_positions(:, 2))
DatasetTmp_10(nx, 14) = min(DatasetTmp_10(nx, 14), min(abs(x - corner_aisle_positions(:, 1))));
end
% Check if the location is within a strong LOS area
if y >= 0 && y <= 5
DatasetTmp_10(nx, 14) = min(DatasetTmp_10(nx, 14), min(y, 5 - x));
elseif y >= 25 && y <= 30
DatasetTmp_10(nx, 14) = min(DatasetTmp_10(nx, 14), min(y - 25, 30 - x));
elseif y >= 50 && y <= 55
DatasetTmp_10(nx, 14) = min(DatasetTmp_10(nx, 14), min(y - 50, 55 - x));
end
end
end
In this modified code, the positions of shelves, corner aisles, and strong LOS areas are stored in separate arrays for easier reference. The Euclidean distance formula (sqrt((x - shelf_positions(:, 1)).^2 + (y - 10).^2)) is used to calculate the distance between each location and the nearest shelf (assuming shelves are located at y = 10).
The code then checks if the location is within a corner aisle or strong LOS area and calculates the corresponding distance if applicable. Finally, the minimum distance is stored in the DatasetTmp_10(nx, 14)
0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Categorie

Scopri di più su Run Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by