Playing with matrix and putting in number values

17 vues (au cours des 30 derniers jours)
Nicle Davidson
Nicle Davidson le 23 Sep 2021
I would like to have a matrix such as X
I have a word ABCD
this word has a sign number 12
The matrix X is made as
X=zeros(5,5)
I have desided that it will be like:
start A B C D
start 0 0 0 0 0
A 0 0 0 0 0
B 0 0 0 0 0
C 0 0 0 0 0
D 0 0 0 0 0
I will go from start to A and put the number 12 in X(start,A) position
then I go from A to B and put the number 12 in X(A,B) position
and so on
the rows are from and columns are to
How can I put the number in those positions, and how can I tell the matrix that for example position X(3,3) should be used and put number in if, in the given word you came to something like BB, or as in this example we have ABCD, how can I tell the matrix that when you come to CD then put a value of 12 in X(4,5)?
The matrix X here needs to understand that if I start a word with A so it put a number 12 in position X(1,2) then I come to AB, so I come from A to B, so the value 12 should be put in X(2,3) and so on until ABCD word is done (which means do this once for BC and once for CD, and save the the matrix X with the new valuses of 12 it god in those positions.
A bit difficult to explain, but I hope it makes sence

Réponse acceptée

Benjamin Kraus
Benjamin Kraus le 23 Sep 2021
There seems to be a few things missing from your description, such as where the number 12 comes from, and how you want to handle the situation where a sequence of letters is repeated (if the string was ABABABA for example). But, this code will do what you indicated, and you will have to work out the remaining details.
word = 'ABCD';
magicNumber = 12;
% Start by assigning a unique index to each unique letter.
[uniqueLetters,~,ind] = unique(word);
X = zeros(numel(uniqueLetters)+1);
% Increment the unique indices to account for the "start" index.
ind = ind(:)'+1;
% Append "1" to the beginning to indicate "start".
ind = [1 ind];
% Loop through the vector of indices, recording the pairings.
for p = 1:numel(word)
% Note this just overwrites the previous value if a pairing of letters
% is repeated.
X(ind(p),ind(p+1)) = magicNumber;
end
disp(X)
% For fun, put this into a table, so you have nice row and column headers.
labels = ["Start"; string(uniqueLetters')];
tbl = array2table(X,"RowNames", labels, "VariableNames", labels);
disp(tbl)
  5 commentaires
Benjamin Kraus
Benjamin Kraus le 23 Sep 2021
@Nicle Davidson Your new question is quite a bit more complicated than your existing question. I suggest you post a new question to MATLAB Answer (you can refer back to this question) with the code you've written so far.
Some tips: If you had mentioned you were interested in the ASCII values, that actually makes things a touch easier. MATLAB character vectors store the ASCII code internally, and you can perform math on those values. To convert the ASCII codes 33 (!) through 126 (~) into the numbers 2 through 94 you can just "subtract" ! from your vector:
word = 'Hello, World!';
double(word) % See the numeric representation (ASCII) for each character.
ans = 1×13
72 101 108 108 111 44 32 87 111 114 108 100 33
word-'!'
ans = 1×13
39 68 75 75 78 11 -1 54 78 81 75 67 0
There are a few options for storing your list of word -> number pairings. My suggestion is just two vectors: One with your words, another with the corresponding numbers. For example:
words = ["matthew", "jonathan", "david"];
numbers = [12 10 5];
I hope that helps you get a little farther along.
Nicle Davidson
Nicle Davidson le 24 Sep 2021
Modifié(e) : Nicle Davidson le 24 Sep 2021
Thank you @Benjamin Kraus for your precious answer, your time and effort.
The addition problem however is left as if we go back to the last question it is needed to add the number to those positions say we have 12 Ellen then X(start,E) should be 12, X(E,l) should be 12 etc.
Then we come to next line in the file, and it is 15 Lenon, so X(start,L) should be 15, X(L,e) should be 15, 'but' now X(e,n) should be 12+15 also 27, then again X(n,o) should be 15 etc.
It will symbolizes how many times for example the combination which starts from 'n' and goes to 'o' is used. In the first word it was used 12 times, the number 12 stands for this. In the second word/or the line of text we read from text document, it has been used 15 times. So therefor we should keep track of how many times the combination is used.
I will make a new post for this, as you mentioned. Do you know how should I refer to this post? Is there any reference number or any special way to do so
I am thankful for your answer as I learnt a lot, and I will choose accept, however the problem is not solved yet.
Any help would be appreciated. Please see the link to the question further:
https://se.mathworks.com/matlabcentral/answers/1459764-working-with-numbers-and-matrix

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by