How do i refer to a column in my imported data and how do i turn all the NaN's into 0's?
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
This is what I started: [numbers,string,raw]=xlsread('mortgagedata',2);
now if I wanted to name each column x1, x2, x3,...x23, how to I do this?
Also the empty cells have been named 'NaN' how do I turn these into e.g. 0 or assign them a letter?
Thanks in advance
0 commentaires
Réponses (2)
KSSV
le 9 Mar 2017
Modifié(e) : KSSV
le 9 Mar 2017
Why there is necessity to name very column x1,x2,......x23 when the data is available in matrix form numbers? You can access your required column using numbers(:,1), numbers(:,3),...etc.
Replacing Nan's with zero:
A = [NaN 1 2 ; NaN NaN 3 ; 4 5 6]
A(isnan(A)) = 0 ;
4 commentaires
Guillaume
le 9 Mar 2017
Do not use numbered variables. It does not simplify things later, it makes them more complicated. Keep your data as a matrix. Everytime you want x1, simply use numbers(:, 1). If you create numbered variables, you can't easily apply the same operation to several variables/columns, you just pollute the workspace with unnecessary variables, and you lose a lot of genericity.
Additionally, since the now ancient R2013b, you can have matrices with named columns in the form of tables. It makes it easy to access columns and it is also much easier to import excel files:
t = readtable('mortgagedata', 2, 'ReadVariableNames', false)
%and if your spreadsheet has a header, change 'ReadVariableNames' to true
%and it'll use the header to name the variables
t.Properties.VariableNames = arrayfun(@(n) sprintf('x%d', n), 1:t.width, 'UniformOutput', false);
Replacing NaN or missing values is also trivial since R2016b:
t = fillmissing(t, 'constant', 0);
The above will work in just one line with a table or matrix but if you create 23 variables, you'll have to type it 23 times. See how creating numbered variables doesn't simplify anything.
0 commentaires
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!