Convert to three dimensional data

Hi guys,
I have this problem for some time which I dont know how to approach and solve. I am trying to create a three dimensional data from a two dimensional. I have data in the format:
C1 C2 C3 C4
1 2 3 10
4 6 9 20
5 4 1 10
...
and variables in C1 are 1st dimension, variables in C2 are 2nd dimension and those in C3 are 3rd dimension. C4 is the value created by three vectors in each dimension, such {1,2,3}=4. Appreciate any suggestion or thoughts.

2 Commenti

Do you mean C1 , C2 and C3 are indices?
Charles Dunn
Charles Dunn il 22 Mar 2016
Modificato: Charles Dunn il 22 Mar 2016
Not exactly sure what you're going for. Are you trying to plot this data in 3D? If so, try below, where your data should already be in a 2D array I called 'C'.
N = 10;
C = rand(N,4);
%get each variable separately
C1 = C(:,1);
C2 = C(:,2);
C3 = C(:,3);
C4 = C(:,4);
%plot data in 3D, with C4 value as the color
scatter3(C1,C2,C3,[],C4);
axis equal tight;
Or are you trying to get a 3D array from your 2D array? Unclear how you would like that to work. You might want to look at reshape(), which can reshape your array dimensions in to as many dimensions as you would like. Note that with an array, it must be an n-dimensional rectangle.

Accedi per commentare.

 Risposta accettata

Stephen23
Stephen23 il 22 Mar 2016
Modificato: Stephen23 il 22 Mar 2016
How to convert row, column, and page indices into an array:
>> M = [1,2,3,10;4,6,9,20;5,4,1,10]
M =
1 2 3 10
4 6 9 20
5 4 1 10
>> out = zeros(max(M(:,1:3)));
>> out(sub2ind(size(out),M(:,1),M(:,2),M(:,3))) = M(:,4);
And checking the locations of where those data have been allocated to:
>> X = find(out); % find non-zero elements
>> out(X) % show their values
ans =
10
10
20
>> [R,C,P] = ind2sub(size(out),X) % and their indices
R =
5
1
4
C =
4
2
6
P =
1
3
9

1 Commento

art
art il 22 Mar 2016
Stephen, thanks very much. It is truly helpful.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Richiesto:

art
il 22 Mar 2016

Commentato:

art
il 22 Mar 2016

Community Treasure Hunt

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

Start Hunting!

Translated by