Confirm the number of dimensions
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Stefan Zeiter
am 22 Apr. 2018
Beantwortet: Image Analyst
am 22 Apr. 2018
Hey
I have a short question. In my excel file (Test) I have defined coordinates for 4 cities.
City x1 x2 x3
1 2 5 4
2 7 2 5
3 12 5 6
4 9 10 5
In this case each city is defined by 3 coordinates (3d). Now I would like to introduce an if condition to plot the coordinates. My goal is to plot the coordinates. When two coordinates are used the script look like this:
data = xlsread('Test.xlsx',1)
dist = dist(data(:,2:3)') % how many coordinates are necessary to describe the location from one city
dist(dist==0) = inf
x1 = data(:,2)
x2 = data(:,3)
x3 = data(:,4)
if ndims(data) == 2
plot(x1, x2, 'x')
else
ndims(data) == 3
plot3(x1, x2, x3, 'x')
end
And when 3 coordinates are used the script looks like this:
data = xlsread('Test.xlsx',1)
dist = dist(data(:,2:4)') % how many coordinates are necessary to describe the location from one city
dist(dist==0) = inf
x1 = data(:,2)
x2 = data(:,3)
x3 = data(:,4)
if ndims(data) == 2
plot(x1, x2, 'x')
else
ndims(data) == 3
plot3(x1, x2, x3, 'x')
end
The problem in my code is that ndims(data) in both cases is 2.
I hope someone can help to solve my problem. In the second case the number of ndims(data) should be 3.
Best
1 Kommentar
David Fletcher
am 22 Apr. 2018
You are only ever going to have 2 dimensions with that code, because that is all that you have specified. In your representation, your are using separate columns of the data matrix to hold the x, y and z co-ordinates. You may well have 3D co-ordinates, but you are still holding them in a 2D matrix.
Akzeptierte Antwort
Image Analyst
am 22 Apr. 2018
Try this:
cityNumber = data(:, 1); numCoordinates = size(data, 2) - 1; % should be 2 or 3 x1 = data(:,2); x2 = data(:,3); if numCoordinates >= 3 x3 = data(:,4); end
if numCoordinates == 2 plot(x1, x2, 'x', 'MarkerSize', 10); caption = '2 Coordinates'; elseif numCoordinates == 3 plot3(x1, x2, x3, '*', 'MarkerSize', 10); caption = '3 Coordinates'; end grid on; xlabel('x1', 'FontSize', 20); ylabel('x2', 'FontSize', 20); if numCoordinates == 3 zlabel('x3', 'FontSize', 20); end title(caption, 'FontSize', 20);
![](/matlabcentral/answers/uploaded_files/114189/0001%20Screenshot.png)
![](/matlabcentral/answers/uploaded_files/114190/0000%20Screenshot.png)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Discrete Data Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!