Playing with matrix and putting in number values

3 views (last 30 days)
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
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

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 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;
% 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);
Nicle Davidson
Nicle Davidson on 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:

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by