Match columns and fill datasets

1 回表示 (過去 30 日間)
Tanmoyee Bhattacharya
Tanmoyee Bhattacharya 2024 年 7 月 11 日
回答済み: Star Strider 2024 年 7 月 12 日
I have two files. One file has some values like that:
1
1
1
1
1
1
2
2
2
2
2
2
2
3
3
3
3
3
3
Another file has values like that
1 10
2 20
3 30
I have to assign values of 1, 2 and 3 from file 2 to file 1. In file 1 if 1 value is 6 then six 10 values will assign. Like that I have 800 values. If there is any shortcut for that.

採用された回答

Star Strider
Star Strider 2024 年 7 月 12 日
Using the accumarray function tthis can be done in one line —
File_1 = [1
1
1
1
1
1
2
2
2
2
2
2
2
3
3
3
3
3
3];
File_2 = [1 10
2 20
3 30];
Out = accumarray(File_1, File_1, [], @(x){File_2(x,:)})
Out = 3x1 cell array
{6x2 double} {7x2 double} {6x2 double}
Result = cell2mat(Out)
Result = 19x2
1 10 1 10 1 10 1 10 1 10 1 10 2 20 2 20 2 20 2 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
disp(Result)
1 10 1 10 1 10 1 10 1 10 1 10 2 20 2 20 2 20 2 20 2 20 2 20 2 20 3 30 3 30 3 30 3 30 3 30 3 30
Out{1}
ans = 6x2
1 10 1 10 1 10 1 10 1 10 1 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Out{2}
ans = 7x2
2 20 2 20 2 20 2 20 2 20 2 20 2 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Out{3}
ans = 6x2
3 30 3 30 3 30 3 30 3 30 3 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFiles and Folders についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by