Putting spaces between elements of a string/

68 visualizzazioni (ultimi 30 giorni)
jeet-o
jeet-o il 4 Gen 2026 alle 22:22
Commentato: Walter Roberson il 5 Gen 2026 alle 20:09
I have found some graph resources that list the adjacency matrices as strings of numbers without spaces, such as:
011001110000
101111000000
110100011000
011010001100
010101000110
110010100010
100001010011
101000101001
001100010101
000110001011
000011100101
000000111110
I would like to insert spaces between each digit, so that I can use it as my adjacency matrix in Matlab. Is there a good way to do this? The matrices will all be square, but of different sizes.

Risposta accettata

John D'Errico
John D'Errico il 4 Gen 2026 alle 23:05
Modificato: John D'Errico il 4 Gen 2026 alle 23:12
A = ['011001110000';'101111000000';'110100011000';'011010001100';'010101000110';'110010100010';...
'100001010011';'101000101001';'001100010101';'000110001011';'000011100101';'000000111110'];
Note that a simple sprintf statement can insert a space between elements in a string.
sprintf('%c ','011001110000')
ans = '0 1 1 0 0 1 1 1 0 0 0 0 '
So if we apply that format to A, we get...
B = sprintf('%c ',A)
B = '0 1 1 0 0 1 1 1 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 0 1 0 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 0 0 0 0 1 1 1 1 1 0 '
B = reshape(B,size(A,2)*2,[])'
B = 12×24 char array
'0 1 1 0 0 1 1 1 0 0 0 0 ' '1 0 1 1 1 1 0 0 0 0 0 0 ' '1 1 0 1 0 0 0 1 1 0 0 0 ' '0 1 1 0 1 0 0 0 1 1 0 0 ' '0 1 0 1 0 1 0 0 0 1 1 0 ' '1 1 0 0 1 0 1 0 0 0 1 0 ' '1 0 0 0 0 1 0 1 0 0 1 1 ' '1 0 1 0 0 0 1 0 1 0 0 1 ' '0 0 1 1 0 0 0 1 0 1 0 1 ' '0 0 0 1 1 0 0 0 1 0 1 1 ' '0 0 0 0 1 1 1 0 0 1 0 1 ' '0 0 0 0 0 0 1 1 1 1 1 0 '
There are probably many ways to have done this, but the above works. Another solution would be to do:
C = repmat(' ',[size(A)].*[1 2]);
C(:,1:2:end) = A
C = 12×24 char array
'0 1 1 0 0 1 1 1 0 0 0 0 ' '1 0 1 1 1 1 0 0 0 0 0 0 ' '1 1 0 1 0 0 0 1 1 0 0 0 ' '0 1 1 0 1 0 0 0 1 1 0 0 ' '0 1 0 1 0 1 0 0 0 1 1 0 ' '1 1 0 0 1 0 1 0 0 0 1 0 ' '1 0 0 0 0 1 0 1 0 0 1 1 ' '1 0 1 0 0 0 1 0 1 0 0 1 ' '0 0 1 1 0 0 0 1 0 1 0 1 ' '0 0 0 1 1 0 0 0 1 0 1 1 ' '0 0 0 0 1 1 1 0 0 1 0 1 ' '0 0 0 0 0 0 1 1 1 1 1 0 '
And again, it works. As I said, many ways...
Looking back at your question though, I think you want to turn the string array into an numeric array, to then be used as an adjacency matrix. And that is trivial. I never needed to go into the earlier artifices at all.
D = A - '0'
D = 12×12
0 1 1 0 0 1 1 1 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 0 0 1 1 0 1 0 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 1 1 1 0 1 0 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 1 0 1 0 1 0 0 0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 0 0 0 0 0 1 1 1 1 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
graph(D)
ans =
graph with properties: Edges: [30×2 table] Nodes: [12×0 table]
plot(graph(D))
  6 Commenti
jeet-o
jeet-o il 5 Gen 2026 alle 19:56
Thank you, That's very instructive - I've spent little time on character strings (like zero!) - I'll bone up on the topic.
Walter Roberson
Walter Roberson il 5 Gen 2026 alle 20:09
Note that something like '1001' is known to MATLAB as being a character vector, whereas "1001" is known to MATLAB as being a character string . The characters in character vectors are individually addressible
A = '0100'
A = '0100'
A(2)
ans = '1'
and you can do arithmetic on them.
A(2) + 2
ans = 51
char(ans)
ans = '3'
whereas fpr character strings indexing refers to an entire group
B = ["0100", "1001"]
B = 1×2 string array
"0100" "1001"
B(2)
ans = "1001"
and the + operator means something different:
B(2) + 2
ans = "10012"

Accedi per commentare.

Più risposte (2)

Matt J
Matt J il 5 Gen 2026 alle 1:21
A=['011001110000'
'101111000000'
'110100011000'
'011010001100'
'010101000110'
'110010100010'
'100001010011'
'101000101001'
'001100010101'
'000110001011'
'000011100101'
'000000111110'];
B=join(string(A-'0'), ' ',2)
B = 12×1 string array
"0 1 1 0 0 1 1 1 0 0 0 0" "1 0 1 1 1 1 0 0 0 0 0 0" "1 1 0 1 0 0 0 1 1 0 0 0" "0 1 1 0 1 0 0 0 1 1 0 0" "0 1 0 1 0 1 0 0 0 1 1 0" "1 1 0 0 1 0 1 0 0 0 1 0" "1 0 0 0 0 1 0 1 0 0 1 1" "1 0 1 0 0 0 1 0 1 0 0 1" "0 0 1 1 0 0 0 1 0 1 0 1" "0 0 0 1 1 0 0 0 1 0 1 1" "0 0 0 0 1 1 1 0 0 1 0 1" "0 0 0 0 0 0 1 1 1 1 1 0"

Catalytic
Catalytic il 5 Gen 2026 alle 1:06
Modificato: Catalytic il 5 Gen 2026 alle 1:06
A=['011001110000'
'101111000000'
'110100011000'
'011010001100'
'010101000110'
'110010100010'
'100001010011'
'101000101001'
'001100010101'
'000110001011'
'000011100101'
'000000111110'];
G=graph(A>'0');
plot(G)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Prodotti


Release

R2025b

Community Treasure Hunt

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

Start Hunting!

Translated by