필터 지우기
필터 지우기

converting a table of numeric data into a double array?

조회 수: 494 (최근 30일)
Abdelrahman Taha
Abdelrahman Taha 2020년 4월 8일
편집: Stephen23 2023년 10월 19일
Hi,
I am trying to convert a table of numeric data to a double array using some of the methods presented before in some answers to similar questions like:
1- table2array function
2- X{:,:}
But they both don't work for me. They change the table into a cell, not a double array

채택된 답변

BN
BN 2020년 4월 8일
I believe it's mean that you have non-numeric data in your table.
Try this:
If T is your table, use cell2mat after table2array.
T_cell = table2array(T);
T_array = cell2mat(T_cell); % T_array is output
% if it is single so after this two lines use:
% T_array = double(T_array)
  댓글 수: 3
BN
BN 2020년 4월 8일
I believe it happens because your first data table includes text and numeric data.
Do you have a problem with using this?
data_numNEW = table2array(data_num );
data_numNEW = cell2mat(data_numNEW ); % T_array is output
I do not see your data but if the problem is you have char instead of number this maybe helps you:
data_numNEW = table2array(data_num );
data_numNEW = cellfun(@double,data_numNEW,'uni',0)
Abdelrahman Taha
Abdelrahman Taha 2020년 4월 8일
편집: Abdelrahman Taha 2020년 4월 8일
Thanks so much. That was really helpful.
I've tried both solutions, the first doesn't work, it gives me this error:
Error using cat
Dimensions of arrays being concatenated are not consistent.
However the second worked, but I needed to convert the contents of the cell array from char to string first and then into double. And then used cell2mat to convert the cell array into a matrix
data_numNEW = table2array(data_num );
data_numNEW = cellfun(@string,data_numNEW,'uni',0);
data_numNEW = cellfun(@double,data_numNEW,'uni',0);
data_numNEW = cell2mat(data_numNEW);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chhanda
Chhanda 2023년 10월 19일
편집: Walter Roberson 2023년 10월 19일
My code is this :
data=readtable('MainData.xlsx');
X1= data(:, 3);% Assuming time, temperature, humidity are columns 3, 4, and 5
X2=data(:, 4);
X3=data(:, 5);
% Convert variables to tables
X1Table = table(X1);
X2Table = table(X2);
X3Table = table(X3);
% Extract the dependent variable (soil moisture)
Y = data(:, 6); % Assuming soil moisture is in column 6
n=height(X1);
onesTable = table(ones(n,1));
a = [onesTable, X1Table, X2Table, X3Table];
c=pinv(a)*Y;
I am getting this error:
Error using svd
First input must be single or double.
Error in pinv (line 18)
[U,s,V] = svd(A,'econ','vector');
Error in Project1 (line 21)
c=pinv(a)*Y;
Kindly suggest a solution.I think i need to change my table to double format ..but unable to do so using table2array().
Kindly help.
  댓글 수: 1
Stephen23
Stephen23 2023년 10월 19일
편집: Stephen23 2023년 10월 19일
"Kindly suggest a solution."
Use numeric arrays for storing basic numeric data (not lots of superfluous nested tables).
Use curly-brace indexing to access table content:
The PINV documentation clearly states that its input must be SINGLE or DOUBLE (i.e. numeric). Instead of doing that, you are providing it with nested tables.
"Convert variables to tables": they are already tables, why nest them inside more tables? Why do you need so many tables? Lets try it without all of those tables, e.g. by using curly-brace indexing:
T = array2table(rand(7,6)); % fake data alternative to READTABLE
Y = T{:,6};
n = height(T);
a = [ones(n,1), T{:,3:5}];
c = pinv(a)*Y % your approach
c = 4×1
0.5321 -0.1422 0.0876 0.2790
c = lsqminnorm(a,Y) % the recommended approach
c = 4×1
0.5321 -0.1422 0.0876 0.2790

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by