How do I create multiple column vectors from one big column vector?

I have two column vectors, objx and objy, both of which contain 20,160 floats. I need to dice it up every 48 iterations to create voronoi figures. There will be 420 voronoi diagrams. How do I go about creating smaller vectors each of which containg 48 floats each to create these voronoi diagrams?

5 件のコメント

Adam Danz
Adam Danz 2020 年 3 月 24 日
"...both of which contain 20,160 floats"
What does that mean? Do you have a 20x1 cell array where each element contains 160 numeric values?
Chad
Chad 2020 年 3 月 24 日
The cell array is 20,160x1, where each cell contains one numeric value
Adam Danz
Adam Danz 2020 年 3 月 24 日
I still don't know what this means: "The cell array is 20,160x1"
Again, it sounds like you're describing a 1x20 or 20x1 cell array where each element of the array contains a 160x1 vector.
Chad
Chad 2020 年 3 月 24 日
The column vectors are both 20160x1, there are 20160 rows and just 1 column, in each vector. Each row only has one numeric element. Its just 20160 numbers stacked on eachother.
Adam Danz
Adam Danz 2020 年 3 月 24 日
That's clear. The comma in 20,160 threw me off. There's no need to use a comma unless the number of digits is very large.

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

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 3 月 24 日
編集済み: Ameer Hamza 2020 年 3 月 24 日

0 投票

Try this
X = reshape(objx, 48, 420); % X is 48*420
Y = reshape(objy, 48, 420); % Y is 48*420
% loop will run 420 times
for i=1:size(X,2)
x = X(:,i); % x is 48*1
y = Y(:,i); % y is 48*1
voronoi(x,y);
% save the voronoi diagram
end

11 件のコメント

Chad
Chad 2020 年 3 月 24 日
This code only produces two 48x1 column vectors, I either need 840 48x1 column vectors or 420 48x2 column vectors. My apologies, I am very new to MATLAB.
Ameer Hamza
Ameer Hamza 2020 年 3 月 24 日
No, the code produces two 48*420 matrices. Both matrices X and Y have 420 columns and each column have 48 elements. The for loop make voronoi diagram using 1st columns of X and Y, then 2nd columns, 3rd columns and so on up to 480th column.
Chad
Chad 2020 年 3 月 24 日
The x and y column vectors which it produces are just the 420th column in the new X and Y columns
Ameer Hamza
Ameer Hamza 2020 年 3 月 24 日
It is a for loop. It will plot 420 diagrams. If you don’t save each diagram, then you will just see the last one. Therefore in place of the comment,
% save the voronoi diagram
You need to use a function such as print() to save each diagram.
Ameer Hamza
Ameer Hamza 2020 年 3 月 24 日
For example write this before end of for loop write
print(['diagram_' num2str(i)], '-djpeg')
Adam Danz
Adam Danz 2020 年 3 月 24 日
編集済み: Adam Danz 2020 年 3 月 24 日
Ameer Hamza 's code is doing what you asked. The x and y columns are not just the 420th column except for the last iteration of the loop.
Two improvements:
1) Don't assume the user will provide a column vector instead of a row vector.
X = reshape(objx(:), 48, 420);
Y = reshape(objy(:), 48, 420);
% ^^^ Forces column vector
2) If you want all 420 plots on the same axes, you'll need hold on
hold on % <-----------------here
for i=1:size(X,2)
x = X(:,i); % x is 48*1
y = Y(:,i); % y is 48*1
voronoi(x,y);
% save the voronoi diagram
end
or maybe you want to create multiple plots, but 420 figures may cause memory problems which is why Ameer suggested you save the plots in the last comment of his code.
for i=1:size(X,2)
x = X(:,i); % x is 48*1
y = Y(:,i); % y is 48*1
figure()
voronoi(x,y);
% save the voronoi diagram
end
Chad
Chad 2020 年 3 月 24 日
Thank you, one last question. Is is possible to save them all as MATLAB figure files instead of jpegs?
Adam Danz
Adam Danz 2020 年 3 月 24 日
編集済み: Adam Danz 2020 年 3 月 24 日
help savefig
Chad
Chad 2020 年 3 月 24 日
Got it, just figured everything out. This was exactly what I needed. Thank you for you patience.
Ameer Hamza
Ameer Hamza 2020 年 3 月 25 日
@Adam, thanks for suggesting the improvements. I am not sure whether it will make any difference for reshaping function that input is a row or column vector, as long as it has just one dimension. For higher-order matrices, we need to be careful about the arrangement of elements.
Adam Danz
Adam Danz 2020 年 3 月 25 日
That's true; a col or row vector will result in the same reshape. My mind may have still been on the possibility of cell arrays converted to matrices.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVoronoi Diagram についてさらに検索

タグ

質問済み:

2020 年 3 月 24 日

コメント済み:

2020 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by