splitting a large column into smaller columns

2 ビュー (過去 30 日間)
Ahmed Al amin
Ahmed Al amin 2018 年 2 月 13 日
回答済み: Ayush Modi 2024 年 8 月 30 日
Hi there, just a simple question, I want to split a column conatiniing 6600 cell into 7 coulmns. who can give a code to make it automatically?

回答 (1 件)

Ayush Modi
Ayush Modi 2024 年 8 月 30 日
Hi Ahmed,
You can use "reshape" function to reshape array by rearranging existing elements. However, "reshape" function requires the number of total elements in the array to be compatible. To make a column of size 7, total elements should be divisible by 7. As 6600 is not divisible by 7, we will need to append elements in the original array to make it compatible. Here is the code to get you started:
data = num2cell(1:6600)'; % Original array
numRows = ceil(length(data) / 7);
numPadding = numRows * 7 - length(data);
paddedData = [data; num2cell(nan(numPadding, 1))];
size(paddedData)
ans = 1x2
6601 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You can use "reshape" function on the padded data, to get the desired array:
reshapedData = reshape(paddedData, [7, numRows])';
size(reshapedData)
ans = 1x2
943 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Refer the following MathWorks documentation for more information on "reshape" function:

カテゴリ

Help Center および File ExchangeGet Started with Optimization Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by