Effacer les filtres
Effacer les filtres

how to use ismember() to check if an inputted number exists in a matrix.

12 vues (au cours des 30 derniers jours)
Tariq Hammoudeh
Tariq Hammoudeh le 2 Déc 2021
Commenté : Walter Roberson le 10 Déc 2021
I have a csv file named "NaiveBankData.csv" (attached) and i imported it, then read it as a matrix, but now how can i check if an inputted number exists in the first colomn of the matrix or not.
the code i used for the first step is:
%import file
importdata('NaiveBankData.csv');
%read file as matrix
readNaiveBankData=readmatrix('NaiveBankData.csv', 'ThousandsSeparator', ',');
then i need to use:
%inputted number
AN = input('Enter account number');
if AN exists in the first colomn of "readNaiveDataBank"
How do i do this last part in code?

Réponse acceptée

DGM
DGM le 3 Déc 2021
This might be a start:
% read file as table, since a matrix can't hold both datatypes
Data = readtable('NaiveBankData.csv');
% inputted number
AN = input('Enter account number: ');
accountexists = ismember(AN,Data.AccountNumber);
if accountexists
accountbalance = Data.Balance(AN==Data.AccountNumber)
end
If you want to do it all with numeric data, you'll have to deal with the fact that the file contains currency data with literal formatting. This can be done using other import tools, but I'm just going to post-process the text in the table.
% read file as table, since a matrix can't hold both datatypes
Data = readtable('NaiveBankData.csv');
% convert to numeric arrays
account = Data.AccountNumber;
balance = cellfun(@(x) x(all(x~=('£,')',1)),Data.Balance,'uniform',false);
balance = str2double(balance);
% inputted number
AN = input('Enter account number: ');
accountexists = ismember(AN,account);
if accountexists
accountbalance = balance(AN==account)
end
  26 commentaires
Stephen23
Stephen23 le 10 Déc 2021
Modifié(e) : Stephen23 le 10 Déc 2021
"I seem to remember @Jan having provided a regexprep() expression that did similar work."
Perhaps on this thread:
Walter Roberson
Walter Roberson le 10 Déc 2021
In the main command window, use Preferences -> Command Window -> Text Display -> Numeric format and select "Long g". Also Preferences -> Variables -> Format -> default array format and select "Long g"
You might have to close the variable browser and re-open it.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by