필터 지우기
필터 지우기

Vectors to Contour3...confused

조회 수: 1 (최근 30일)
John
John 2017년 8월 25일
댓글: Vaidyanathan Thiagarajan 2017년 8월 28일
I have 3 linked vectors from physical data: call them vectors a,b,c. A given cell entry across a,b,c are linked (taken at the same time).
I'd like to plot this data as a 3D contour:
x1=a(1), y1=b(1), z1=c(1) is a coordinate point to plot
x2=b(2),... are another coordinate point, etc.
How can I make the right Z matrix? Matlab examples frustratingly always have computable functions which don't help.
I've read 15-20 posts on this but have clear answer.
  댓글 수: 1
Vaidyanathan Thiagarajan
Vaidyanathan Thiagarajan 2017년 8월 28일
Hello Paul,
I believe this is what you are looking for :
%1D vector that defines the x,y, and z coordinates
x = [0.2 0.4 0.6 1.0 0.2 0.4 0.6 1.0 0.2 0.4 0.6 1.0 0.2 0.4 0.6 1.0];
y = [0.25 0.25 0.25 0.25 0.5 0.5 0.5 0.5 0.75 0.75 0.75 0.75 1.0 1.0 1.0 1.0];
z = x.^2 + y.^2;
%Now, reshape all of them into a 2D matrix as required by the contour3 function
XMat = reshape(x,4,4);
YMat = reshape(y,4,4);
ZMat = reshape(z,4,4);
%Plot the contour
contour3(XMat,YMat,ZMat,30);
As the documentation indicates, the following are the conditions on X,Y, and Z :
a. If X and Y are vectors, then length(X) must equal size(Z,2) and length(Y) must equal size(Z,1). The vectors must be strictly increasing or strictly decreasing and cannot contain any repeated values.
b. If X and Y are matrices, then their sizes must equal the size of Z. Typically, you should set X and Y so that the columns are strictly increasing or strictly decreasing and the rows are uniform (or the rows are strictly increasing or strictly decreasing and the columns are uniform).
This means that if your 1D x,y, and z vectors don't obey this condition, then you might not get the desired result. Hence, it is first necessary to induce an order to the 1D vectors and then reshape them to 2D matrices appropriately before passing it on to the contour3 function.
In the above code snippet, I have directly converted the 1D arrays into equivalent 2D matrices using the 'reshape' function as my data is already ordered. However, in your case, you might have to induce an order first before reshaping.
For more details on reshape function, please refer to the following link :
Hope this helps.
Vaidyanathan

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by