converting a table of numeric data into a double array?
Show older comments
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
Accepted Answer
More Answers (1)
Chhanda
on 19 Oct 2023
Edited: Walter Roberson
on 19 Oct 2023
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 Comment
"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 = lsqminnorm(a,Y) % the recommended approach
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!