フィルターのクリア

How to put vectors inside a matrix

190 ビュー (過去 30 日間)
tabw
tabw 2014 年 8 月 28 日
回答済み: Aline 2014 年 8 月 28 日
For example, I have 100 vectors. I want to put all 100 vectors inside a 10X10 matrix.
  1 件のコメント
Ben11
Ben11 2014 年 8 月 28 日
Do you mean you have 100 1x1 vectors?

サインインしてコメントする。

採用された回答

Andrew Reibold
Andrew Reibold 2014 年 8 月 28 日
編集済み: Andrew Reibold 2014 年 8 月 28 日
You can make a 3d array where each slot from a 10x10 matrix has its own vector with the following syntax.
matrix(1,1,:) = vector1;
matrix(1,2,:) = vector2;
matrix(1,3,:) = vector3;
......
matrix(2,1,:) = vector11;
......
matrix(10,8,:) = vector98;
matrix(10,9,:) = vector99;
matrix(10,10,:) = vector100;
I strongly recommend using a for loop if your vectors are named well enough that you can call them in sequence.
For example, if your vectors were really named 'vector1' through 'vector100', you could do the following and Matlab would do amazing and magical things for you.
for dim1 = 1:10
for dim2 = 1:10
matrix(dim1, dim2, :) = eval(['vector',num2str((10*(dim1-1)+dim2))])
end
end
I didn't personally test that, so if it gives you an error let me know and I'm sure its an easy fix or some minor typo I made. Just know its doable so you dont waste your time!
If you need additional help, feel free to comment below and we will see if I or someone can help you. If this answered your question.. yay!

その他の回答 (1 件)

Aline
Aline 2014 年 8 月 28 日
Do you want to create a 3D Matrix?
A way to do that (although possibly not the fastest) would be in a for loop and allocating each vector at a time.
You can assign the vector to the 3rd dimension like this:
A(ii,jj,:) = v1;
Though, depending on what you want to do with the vectors later, it might be easier to access them in a cell array.
C = {v1, v2; v3, v4}

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by