简体   繁体   中英

How do I input x,y,z co-ordinates as a single element inside a matrix?

I am trying to create a bezier surface using MATLAB code. For this I have to input co-ordinates in the form of [[x1 y1] [x2 y2] [x3 y3];[x4 y4] [x5 y5] [x6 y6]]. I have tried using cell array but arithmetical operations with other matrices or array isn't possible while using cell array. Please help example:

C=[[2 3] [3 4] [4 5] [5 6];[2 5] [5 2] [7 8] [8 9]];
A=C(1,3);
ans=[4 5]

Also

C=[[2 3] [3 4] [4 5] [5 6];[2 5] [5 2] [7 8] [8 9]];
D=[1 2;2 1;3 1;2 3];
E=C*D
ans=[[30 38] [26 33];[49 51] [40 47]]

you can try using cat(3,..) :

C = cat(3,[[2 3] ;[3 4] ;[4 5]; [5 6]],[[2 5]; [5 2] ;[7 8] ;[8 9]]);
A = C(3,:,1)

You could use a 3D matrix, with the second "layer" being your second coordinate pair, or simply use 2 matrices!

Using your example:

C1 = [2 3 4 5; 2 5 7 8];
C2 = [3 4 5 6; 5 2 8 9];
D = [1 2; 2 1; 3 1; 2 3];
E1 = C1*D; E2 = C2*D;

In 3D matrices:

% Make 3D matrix of same size as C1 but 2 layers
C = zeros([size(C1), 2]);
C(:,:,1) = C1; C(:,:,2) = C2;
E = cat(3, C(:,:,1)*D, C(:,:,2)*D);
% ans is a 3D matrix, with the 2 layers representing the pairs in your example.

Indexing the 3D matrix like you wanted:

C13 = reshape(C(1,3,:),1,2) % C13 = [4, 5] 
% or
C13 = squeeze(C(1,3,:))'    % C13 = [4, 5]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM