Keep getting "unable to use a value of type string as an index" error and I'm unsure why
28 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
As stated in the title I keep getting "Unable to use a value of type string as an index" error for one line of code and I'm unsure what I am doing wrong. The code is as follows:
sc1 = [sc(1:end,"custID"),sc(1:end,"CLAGE"), sc(1:end,"DELINQ") , sc(1:end,"DEROG") , sc(1:end,"DEBTINC"), sc(1:end,"VALUE") , sc(1:end,"NINQ"), sc(1:end,"BAD")]; %%% This is the line that is giving me the error
scorecard1 = creditscorecard(sc1,"IDVar","custID","BinMissingData",true);
scorecard1 = modifybins(scorecard1,'DEBTINC','CutPoints',[ 30 40]);
scorecard1 = modifybins(scorecard1,'VALUE','CutPoints',[50000 100000 150000]);
scorecard1 = modifybins(scorecard1,'DEROG','CutPoints', [1 2 3]);
scorecard1 = modifybins(scorecard1,'DELINQ','CutPoints', [1 2 3]);
scorecard1 = modifybins(scorecard1,'CLAGE','CutPoints',[100 200 300]);
scorecard1 = modifybins(scorecard1,'NINQ','CutPoints',[1 2 3]);
scorecard1 = fitmodel(scorecard1);
Thanks!
1 Commento
Jan
il 29 Apr 2022
What is sc? Why do you assume, that "sc(1:end,"custID")" is a valid syntax?
Please format your code using the tools above the section for editing. Then it would be easier to seem which line is failing.
Risposte (1)
Hari
il 3 Ott 2023
Hi Adian,
I understand that you are encountering an error related to using a value of type string as an index in MATLAB.
I assume that ‘sc’ is a matrix. To resolve this issue, you can modify your code to convert the matrix into a table using the “array2table” function. This allows for indexing using string column names without encountering the error. Here is an example of how you can do that:
% Convert sc to a table if it is not already
if ~istable(sc)
sc = array2table(sc); % Assuming sc is a matrix, convert it to a table
end
Once ‘sc’ is converted to table you can perform indexing operations using strings.
% Perform the indexing operation
sc1 = [
sc(:, "custID"),
sc(:, "CLAGE"),
sc(:, "DELINQ"),
sc(:, "DEROG"),
sc(:, "DEBTINC"),
sc(:, "VALUE"),
sc(:, "NINQ"),
sc(:, "BAD")];
When I did the above modification, your code ran without errors and produced output.
Output observed:
Generalized linear regression model:
logit(BAD) ~ 1
Distribution = Binomial
Estimated Coefficients:
Estimate SE tStat pValue
________ _______ _______ _______
(Intercept) 0.40547 0.91287 0.44416 0.65692
5 observations, 4 error degrees of freedom
Dispersion: 1
Refer to the documentation of “array2table” function for more information.
Hope this helps!
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!