How to take the square of every single value in the code below

1 回表示 (過去 30 日間)
muhammad choudhry
muhammad choudhry 2022 年 6 月 17 日
コメント済み: Voss 2022 年 6 月 18 日
Hi,
I am using the code below in which the line
col_13_data(:,i) = table2array( data(:,13) );
producing 48x79 double
I want to take the square of each value that is produced, how would I proceed this.
I am pasting the code below with the error I am encountering.
Code:
close all; clear all; clc;
P = 'F:\3-PIV_Experimental_Data\Calculations_TurbulentIntensity\line_Data\Elliptical_Side_LSB\Length\DesignPoint\110_outlet';
Q = 'F:\3-PIV_Experimental_Data\Calculations_TurbulentIntensity\line_Data\Elliptical_Side_LSB\Length\DesignPoint';
S = dir(fullfile(P,'*.csv'));
N = natsortfiles({S.name});
TurbulentFluctuationArray_Mean=zeros(numel(N), 1);
col_13_data = zeros(48,numel(N))
col_13_data_Square = zeros(48,numel(N))
for i = 1:numel(N);
data = readtable( fullfile(P, N{i}) ); % read the csv files
col_13_data(:,i) = table2array( data(:,13) ); % get the 13th column of each file
col_13_data_Square = table2array( data(:,13).^2 )
end
Error:
Operator '.^' is not supported for operands of type 'table'.
Error in Extracting_Column (line 15)
col_13_data_Square = table2array( data(:,13).^2 )

採用された回答

Voss
Voss 2022 年 6 月 18 日
編集済み: Voss 2022 年 6 月 18 日
Use {} to get the values out of the table:
for i = 1:numel(N);
data = readtable( fullfile(P, N{i}) ); % read the csv files
col_13_data_Square(:,i) = data{:,13}.^2;
end
  2 件のコメント
muhammad choudhry
muhammad choudhry 2022 年 6 月 18 日
that's perfectly work. Just wondering is there specific reason it did not work with () but worked with the {} brackets.
Voss
Voss 2022 年 6 月 18 日
Using parentheses () to index a table gives you another table:
t = table([1;2;3],[4;5;6]) % a table with two columns
t = 3×2 table
Var1 Var2 ____ ____ 1 4 2 5 3 6
new_t = t(:,2) % a table with one column
new_t = 3×1 table
Var2 ____ 4 5 6
class(new_t)
ans = 'table'
On the other hand, using curly braces {} extracts the values from the column(s) of the table:
new_t = t{:,2} % a numeric column vector
new_t = 3×1
4 5 6
class(new_t)
ans = 'double'

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by