I need to find the name score of the names in a text file.
name point: David --> 4 1 22 9 4 = 4+1+22+9+4 = 40
Thanks for your help.

댓글 수: 2

dpb
dpb 2021년 1월 29일
What have you tried?
Philippe Lebel
Philippe Lebel 2021년 1월 29일
as there are many solutions posted that answer your question, i'd suggest to mark your question as answered.

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

 채택된 답변

Mathieu NOE
Mathieu NOE 2021년 1월 29일

2 개 추천

hello
this code does the trick :
ascii_value = uint32(yourstring); % convert your char / string into corresponding ASCII value
% let's converts ASCII values into values ranging from 1 to 26 (use offsets)
% uppercase letters
ind = find(ascii_value<91);
UL = ascii_value(ind) - 64;
% lowercase letters
ind = find(ascii_value>96);
LL = ascii_value(ind) - 96;
value = sum(UL) + sum(LL); %

댓글 수: 4

Pax Azx
Pax Azx 2021년 1월 29일
thanks but I have to do this for thousands of names in the txt file.
Mathieu NOE
Mathieu NOE 2021년 1월 29일
ok so let's create a function and loop for all words extracted from a sentence
here is it
sentence = 'His story is told in the first book of Samuel and his life as a king in the second book of Samuel and at the beginning of the First book of Kings';
STR = split(sentence);
for ci = 1:numel(STR)
value(ci) = subfct(char(STR(ci)));
end
function value = subfct(yourstring)
ascii_value = uint32(yourstring); % convert your char / string into corresponding ASCII value
% let's converts ASCII values into values ranging from 1 to 26 (use offsets)
% uppercase letters
ind = find(ascii_value<91);
UL = ascii_value(ind) - 64;
% lowercase letters
ind = find(ascii_value>96);
LL = ascii_value(ind) - 96;
value = sum(UL) + sum(LL); %
end
Pax Azx
Pax Azx 2021년 1월 29일
I wrote to you. Thank you if you help.

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

추가 답변 (2개)

Jan
Jan 2021년 1월 29일
편집: Jan 2021년 1월 31일

1 개 추천

Name = 'David';
Num = lower(Name) - 'a' + 1; % 'a' -> 1, 'b' -> 2, ...
Score = sum(Num);
[EDITED] You mention in comments, that you want to do this for "names in a text file". Do the files contain one name per line?
Str = fileread('YourTextFile.txt');
Str(Str == char(13)) = [];
CStr = strsplit(Str, char(10));
Score = zeros(size(CStr));
for k = 1:numel(CStr)
Score(k) = sum(lower(CStr{k}) - 'a' + 1);
end

댓글 수: 4

I cannot fight against Jan, he's really too good ...
Score = sum(Num(Num>0)); % just avoid taking blanks into account
Pax Azx
Pax Azx 2021년 1월 29일
There is one name on each line and there are 18239 names in total.
Jan
Jan 2021년 1월 30일
@Mathieu NOE: I'm very happy that we are fighting together to get the best solution for the question. Seeing different approachs is useful to understand the power of MATLAB.
Mathieu NOE
Mathieu NOE 2021년 2월 1일
@Jan : tx for your nice comments , but I'm not yet to your level - still I like to see how tricky problems can be solved elegantly by knowledgable people like you and others contributors here.

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

Philippe Lebel
Philippe Lebel 2021년 1월 29일

0 개 추천

im late to the show, but here we go anyway...
clear
clc
name = 'david';
a = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
total = 0;
for i=1:length(name)
arr = cellfun(@(x)isequal(x,name(i)),a);
[row,col] = find(arr);
total = total + col
end

카테고리

도움말 센터File Exchange에서 Convert Image Type에 대해 자세히 알아보기

질문:

2021년 1월 29일

댓글:

2021년 2월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by