Replacing each array element with a series of values
古いコメントを表示
Hi, I have a a 1 x N matrix of numbers. For example, a matrix T:
T = [1 4 7]
I would like to convert T to the new matrix as follows:
T_new = [1 2 3 4 5 6 7 8 9]
Basically, each element of T is replaced by an arithmetic series of 3 values with that element as the starting element and common difference = 1.
I tried
T_new = [T:T+3];
But it only updates the first element (I get T_new = [1 2 3 4]). Is there any quick way to do this without using a for loop ?
Thanks.
採用された回答
その他の回答 (2 件)
Andrei Bobrov
2017 年 4 月 25 日
編集済み: Andrei Bobrov
2017 年 4 月 25 日
T_new = reshape(T(:)' + (0:2)',1,[])
3 件のコメント
Varun Nair
2017 年 4 月 26 日
Andrei Bobrov
2017 年 4 月 26 日
You are using an older version of MATLAB.
Variant for you:
T_new = reshape(bsxfun(@plus,T(:)',(0:2)'),1,[])
Varun Nair
2017 年 4 月 29 日
dbmn
2017 年 4 月 25 日
One possible solution would be:
0. having
T = [1 4 7];
1. create a temporary matrix T_temp that looks like (this could be done in a loop very easily)
T_new = [1 2 3;
4 5 6;
7 8 9 ];
2. then reshape that matrix using
T_new=reshape(T_tmp', 1, numel(T_tmp));
1 2 3 4 5 6 7 8 9
1 件のコメント
Varun Nair
2017 年 4 月 25 日
編集済み: Varun Nair
2017 年 4 月 25 日
カテゴリ
ヘルプ センター および File Exchange で Resizing and Reshaping Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!