Assign same number of elements to the column data based on number of elements (each row) of the other column

1 回表示 (過去 30 日間)
Hi all, I have two column data (Please refer to the attached) where first column contains "Number of Elements" and the second column contains "Stages" data. I want to assign the same number of data elements of "Stages" data to the new column ("Required") column based on the "Number of Elements" (first column) data. Any help in this regard is highly appreciated. I have included sample data as well.
Note: Desired data does not exist and I want it to be created as third column. I need the third column result based on first two column data.

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 4 月 5 日
編集済み: Walter Roberson 2022 年 4 月 5 日
This code creates the desired data.
I notice that the desired data already appears to exist inside the file -- though it might perhaps need to be converted from character. Is the actual requirement to convert the existing file, or is it to generate new output without reference to that existing column?
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/952839/Sample_Data.xlsx';
T = readtable(filename, 'VariableNamingRule', 'preserve');
R = rowfun(@(NE, S) {repmat(S, 1, NE)}, T, 'InputVariables', 1:2, 'SeparateInputs', true, 'OutputFormat', 'table', 'OutputVariableName', 'Required')
R = 20×1 table
Required _______________________ {[ 0 0]} {[ 0]} {[ 0]} {[ 1 1]} {[ 1]} {[ 2]} {[ 2 2 2 2 2]} {[2 2 2 2 2 2 2 2 2 2]} {[2 2 2 2 2 2 2 2 2 2]} {[ 3 3]} {[ 0 0 0 0 0 0]} {[ 3 3]} {[ 3]} {[ 1 1 1 1 1 1 1 1 1]} {[1 1 1 1 1 1 1 1 1 1]} {[ 1 1 1]}
  1 件のコメント
Ganesh Naik
Ganesh Naik 2022 年 4 月 5 日
編集済み: Ganesh Naik 2022 年 4 月 5 日
Hi Walter, thanks. Desired data does not exist and I want it to be created as third column. I should have made it clear earlier, sorry about it. I need the third column result based on first two column data. I have edited the same info in the orignal question. Thanks

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by