How to convert a matrix into a 3d matrix

조회 수: 153 (최근 30일)
Giovanni
Giovanni 2013년 12월 11일
댓글: Giovanni 2013년 12월 12일
Hi i have a function that is creating a matrix. How do i create a vector that takes this matrix and adds n dimensions to it, i.e makes it 3d???
  댓글 수: 3
Jos (10584)
Jos (10584) 2013년 12월 11일
What do you exactly mean by "a vector that takes this matrix and adds n dimensions"?
Tip: if English is a problem when you're not a native speaker (like me), a question is often clarified by adding a short example.
Image Analyst
Image Analyst 2013년 12월 11일
Do you mean "a function" instead of "a vector"???

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

채택된 답변

sixwwwwww
sixwwwwww 2013년 12월 11일
편집: sixwwwwww 2013년 12월 11일
here is an example to repeat a matrix N times in the 3rd dimension
a = rand(10); % your matrix
b = repmat(a, 1, 1, 5); % makes multiple copies of your matrix in 3rd dimension
I hope it helps. Good luck!

추가 답변 (3개)

Giovanni
Giovanni 2013년 12월 11일
Thanks guys. What i currently have is a function which produces n lots of a vector Time, but its values are different each time. I want to combine all this vectors into one 3d matrix, does this help? Thanks in advance
  댓글 수: 2
sixwwwwww
sixwwwwww 2013년 12월 11일
how do you want to combine them in 3D how they should be combined?
Giovanni
Giovanni 2013년 12월 11일
thanks for your help man. When i ran my loop i will produce say 5 matrix. Then i want to put all this together in a 3d vector

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


Image Analyst
Image Analyst 2013년 12월 11일
편집: Image Analyst 2013년 12월 11일
If you're in a loop, you can do
for k = 1 : numberOfLoopIterations
thisMatrix = yourFunction();
if k == 1
% Start it/initialize it.
matrix3D = thisMatrix;
else
% Append this matrix in 3D to the "master" one.
matrix3D = cat(3, matrix3D, thisMatrix);
end
end
  댓글 수: 5
Giovanni
Giovanni 2013년 12월 11일
oh i get it now. However now i have a new problem. Can you concatenate vectors with different dimension into one 3d vector?
Image Analyst
Image Analyst 2013년 12월 11일
No, the array must be rectangular. You can't have any "ragged" edges. If you need that, then you need to use cell arrays, which is a lot more complicated). See http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

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


Walter Roberson
Walter Roberson 2013년 12월 12일
As you asked the question again, I will repeat what Image Analyst said, but with different wording:
In order to combine vectors unchanged into a numeric matrix, the vectors must all be the same length. There is no way to overcome this limitation in a numeric matrix.
You can chop all the vectors down to the same length, by throwing away information. After that you can use cat() to combine them into a numeric matrix.
You can extend the short vectors with some value (such as 0 or inf or NaN) so that the vectors are all the same length. After that you can use cat() to combine them into a numeric matrix.
Now another option: if you have several vectors of coordinates (such as time) and corresponding values, then you can determine some "representative" coordinates and use interp1() to interpolate the values at the representative coordinates, for each vector. After that you can use cat() to combine the interpolated values into a numeric matrix. For example:
L1 = length(v1); L2 = length(v2); L3 = length(v3);
new_L = mean([L1, L2, L3]); %number of points we will interpolate at
all_v = vertcat(v1(:), v2(:), v3(:)); %combine all of the vectors
minv = min(all_v);
maxv = max(all_v);
new_v = linspace(minv, maxv, new_L); %equally spaced values between the min and max
new_y1 = interp1(v1, y1, new_v);
new_y2 = interp1(v2, y2, new_v);
new_y3 = interp1(v3, y3, new_v);
combined_y = vertcat(new_y1(:), new_y2(:), new_y3(:));
plot(new_v, combined_y)
  댓글 수: 2
Giovanni
Giovanni 2013년 12월 12일
Yeah i know i have to make my vectors the same dimension, but how do i do this? How will i crop all my vectors to the min number of rows of one vector? I really have no idea man, I am not being lazy
Giovanni
Giovanni 2013년 12월 12일
oh and if you are not going to help, may you please stop flagging my questions?? maybe someone else is willing to help me

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by