Confirm the number of dimensions
조회 수: 3 (최근 30일)
이전 댓글 표시
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
David Fletcher
2018년 4월 22일
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.
채택된 답변
Image Analyst
2018년 4월 22일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!