How do I take the power of elements in a table?

1 回表示 (過去 30 日間)
T4H14
T4H14 2018 年 4 月 20 日
編集済み: Peter Perkins 2018 年 4 月 20 日
I have a table called NYLD1 and I want to create some new variables that are the squares and cubes of the variables columns 29:32 so I wrote the following:
cur_varnames = NYLD1.Properties.VariableNames;
for K = [29:32]
thisvarname = cur_varnames{K};
thisval = NYLD1.(thisvarname);
sq_varname = [thisvarname '2'];
cu_varname = [thisvarname '3'];
NYLD1.(sq_varname) = thisval .^ 2;
NYLD1.(cu_varname) = thisval .^ 3;
end
However, I get the following error when I run this:
Undefined function 'power' for input arguments of type 'table'.
How can I fix this?
  1 件のコメント
Peter Perkins
Peter Perkins 2018 年 4 月 20 日
編集済み: Peter Perkins 2018 年 4 月 20 日

It's funny. Some people seem to gravitate to using dot subscripting, some to braces. They both do "contents of", but I always suggest dot for one at a time, braces for multiple at a time. Nothing to do with your real question, but in this case I'd suggest

names = NYLD1.Properties.VariableNames(29:32);
sq_names = ...
cu_names = ...
NYLD1{:,sq_names} = NYLD1{:,names}.^2;
NYLD1{:,cu_names} = NYLD1{:,names}.^3;

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

採用された回答

Peter Perkins
Peter Perkins 2018 年 4 月 20 日
Based on the error you cite, it seems that one or more of those variables are themselves tables. Nothing wrong with that, you'll just have to do something slightly different.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by