Matrix "addition" qeustion

2 ビュー (過去 30 日間)
T4H14
T4H14 2017 年 10 月 10 日
回答済み: Guillaume 2017 年 10 月 11 日
How do I create a matrix A which is equal to a matrix X with column vectors added onto it which are the element wise square and cube of some of the columns of X? I have a matrix that is 32413x46 and I want to take columns 12-18, square them and add them as columns 47-53 to the previous matrix and then take those same columns 12-18, cube them and add them on as columns 54-60.
  1 件のコメント
jean claude
jean claude 2017 年 10 月 10 日
can you give a numerical example ?

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

採用された回答

Guillaume
Guillaume 2017 年 10 月 11 日
Right, you don't have a matrix but a table. Using correct term avoids wasting everyone time. matrices and tables are two completely different things.
All columns of a table must be named. I question the wisdom of adding so many columns to a table, particularly as the value are trivial to calculate whenever you need them, but if it needs to be done, the simplest thing would be to prepend (or append) the names of the existing columns with 'squared' and 'cubed' respectively. This is easily done:
yourtable = array2table(randi([0 100], 20, 46)); %demo table
newtable = [yourtable, ...
array2table(yourtable{:, 12:18} .^2, 'VariableNames', compose('squared_%s', string(yourtable.Properties.VariableNames(12:18)))), ...
array2table(yourtable{:, 12:18} .^3, 'VariableNames', compose('cubed_%s', string(yourtable.Properties.VariableNames(12:18))))]

その他の回答 (2 件)

Guillaume
Guillaume 2017 年 10 月 10 日
If I understood correctly:
A = X;
A(:, 47:53) = X(:, 47:53) + X(:, 12:18) .^ 2;
A(:, 54:60) = X(:, 54:60) + X(:, 12:18) .^ 3;

Matthew
Matthew 2017 年 10 月 10 日
Here's one possible answer to your question,
%I have a matrix that is 32413x46
X = rand(32413,46);
%and I want to take columns 12-18, square them and add them as columns 47-53 to the previous matrix
X(:,47:53) = X(:,12:18).^2;
%and then take those same columns 12-18, cube them and add them on as columns 54-60.
X(:,54:60) = X(:,12:18).^3;
Another possibility - if you mean "add" literally (as Guillaume answered above)
%I have a matrix that is 32413x46
X = rand(32413,46);
%and I want to take columns 12-18, square them and add them as columns 47-53 to the previous matrix
X(:,47:53) = X(:,12:18).^2 + X(47:53);
%and then take those same columns 12-18, cube them and add them on as columns 54-60.
X(:,54:60) = X(:,12:18).^3 + X(54:60);
  2 件のコメント
T4H14
T4H14 2017 年 10 月 10 日
You are right. I meant concatenate rather than add.
T4H14
T4H14 2017 年 10 月 10 日
Unfortunately I actually have a 32413x46 Table rather than a Double so it won't let me perform that operation. Can I instead individually concatenate the square of column variables? Do I have to then name these new columns too?

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by