how to add space between matrix elements?

hi guys am working on binary matrix, and i want a code to add space between matrix elements?
matrix as :
11001110;11001111;00011100;111000010
some matrix are (33X33)

댓글 수: 2

Oleg Komarov
Oleg Komarov 2014년 12월 14일
What do you mean by adding space?
Jan
Jan 2014년 12월 14일
편집: Jan 2014년 12월 14일
The shown data is not "binary matrix". Please explain the type and dimension of the data. Is it a CHAR matrix or a cell string? Is it a UINT8 array? Which matrix is 33x33?

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

 채택된 답변

David Young
David Young 2014년 12월 14일

2 개 추천

So another guess is that the input is in fact a double matrix, but the idea is that there ought to have been spaces between the original characters from which it was read.
That is,
m = [11001110;11001111;00011100;11100001]; % extra 0 omitted at end
but we would have liked to have had
mCorrect = [1 1 0 0 1 1 1 0; 1 1 ... ];
If so, m can be converted to a cell array of strings with
mstr = arrayfun(@(x) sprintf('%08u', x), m, 'UniformOutput', false);
and then to a double array but only with the values 0 and 1 stored in it with
mbin = cell2mat(cellfun(@(s) s - '0', mstr, 'UniformOutput', false));
If, however, you want a cell array of strings with spaces in you can do more or less what Guillaume said:
mwithspaces = regexprep(mstr, '.(?=.)', '$0 ');

댓글 수: 3

sara
sara 2014년 12월 15일
편집: sara 2014년 12월 15일
it works fine for small matrix, but when i used that code for large matrix (33X117) it gave different answer which is:
1 -2 1 0 0 1 1 1 53 -5 0 3 2
1 -2 1 0 0 1 0 0 53 -5 0 3 0
1 -2 1 1 1 1 0 0 53 -5 0 3 2
1 -2 0 1 1 0 1 1 53 -5 0 3 2
1 -2 1 1 0 0 1 0 53 -5 0 2 8
1 -2 1 0 1 0 0 1 53 -5 0 3 0
1 -2 1 0 1 0 1 0 53 -5 0 3 1
1 -2 1 1 0 1 0 1 53 -5 0 3 0
and when i change the code :
mstr = arrayfun(@(x) sprintf('%08u', x), m, 'UniformOutput', false);
to:
mstr = arrayfun(@(x) sprintf('%033u', x), m, 'UniformOutput', false);
it didn't work too.....it gave a mixture of binary and decimal instead of being 0 & 1 ,, how to solve it?
Guillaume
Guillaume 2014년 12월 15일
You still haven't told us what form your input matrix takes. So it's difficult to tell you how to solve your issue.
Also, as a rule don't accept an answer if it doesn't fully answer your question. People don't tend to look at posts once an answer has been accepted.
David Young
David Young 2014년 12월 15일
Yes, as Guillaume says, it's difficult to work out the solution without knowing the input. Can you show us how the large matrix is defined?

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

추가 답변 (1개)

Guillaume
Guillaume 2014년 12월 14일

2 개 추천

It's always a good idea to give an example of inputs that is a valid matlab expression and an example of output.
Assuming your input is a 2D array of char and that you want to add spaces between columns:
m = ['11001110';'11001111';'00011100';'11100001'] %had to remove a 0 from the end of the last element in your example
mwithspaces = cell2mat(regexprep(num2cell(m, 2), '.(?=.)', '$0 '))

댓글 수: 8

sara
sara 2014년 12월 14일
it didn't work, it keeps giving this mess:
??? Error using ==> regexprep All cells for regexprep must be strings.
Error in ==> asa at 4 mwithspaces = cell2mat(regexprep(num2cell(m, 2), '.(?=.)', '$0 '))
Guillaume
Guillaume 2014년 12월 14일
As stated, it assumes the input is a 2D array of char.
Your input obviously isn't. What is it?
sara
sara 2014년 12월 15일
hi Guillaume , the output which i was expecting is
mCorrect = [1 1 0 0 1 1 1 0; 1 1 ... ];
just adding space between matrix elements.
Guillaume
Guillaume 2014년 12월 15일
The 'spaces' between the elements in mCorrect are just spaces that matlab prints when you display the matrix. They're not part of the matrix.
So I guess your question is how to generate that matrix of zeros and ones. We need to know the exact form of your input to be able to answer that.
Or possibly more relevant, is how do you get your 'wrong' matrix in the first place? Most likely, you can generate the 'right' one to start with.
sara
sara 2014년 12월 16일
편집: sara 2014년 12월 16일
its a simple matrix which i want to calculate the distance between its elements am new in matlab so here is the code:
m = [ 110011100010101011100010101010111; 001100100000010010101001001000101; 111110000100011000011010100101110; 101101101011010010001011010110110; 000011100100001100001110101100000; 001101001110100010100110110101010; 011010100101001101101000111011110; 001110101101000000010110001111010; 010001111010000110001101000010010; 010010101010000000010101100001010; 011001010100110100001011001001110; 000101100000011010100100000001000; 000100001000101110100010000010000; 100101111000000110001111110110000; 011011001001100010111100110101110; 100101000000011100100001010011110; 010111110110100101000100100101111; 001111110001101100001110110100110; 011000010101100010010110110011010; 010010100110010101001000100101001; 000010000001110110111010100011010; 011111001110000010101000000000110; 011001000110101110001101101100110; 110010100110101010000001011110110; 111100110110101110011010111010000; 100010011011010000011101100011110; 000111111000011010001001100100101; 001011011001001000011010101001010; 110111011100110010111111111001000; 011110100100000100100101100110110; 111100010001000000101100000011100; 110101111001101100000010001111110; 010100101110010001100110100001000; 101101100110100010100010101100010; 010010001100111100100101110001111; 101011001101010001101000110110111; 100011101101101100001010111000110; 000111001100101010101101010000101; 101011100110010000010111001100010; 111100111010010000010110001111010; 110000110011001000100000110000101; 101000110011001100000001110000010; 000101000001111000001011001110000; 011010000001011110000100010111000; 000101111011000000111100001100010; 001101011110001110001000100011110; 111111101100111000000111111000101; 101110011101011000000001110110010; 001101001110110110000100000011000; 010010111010110000100110101000111; 011010001111110110101101001010110; 000101010001010010010011100111010; 011010001011101001011110111001011; 101000011101100001110110010110010; 000101100011100010100001111010101; 010111000111010101011110100110010; 001100011101100011000100001100010; 100011001011010000001110111000101; 110100000010010111100101110010010; 100001100011111000010011001110010; 110111101110001011001010101111010; 001111010110000100000111000111110; 101001110110000101011010110000010; 100011011101110100111110101000010; 000110100001110000101001100111010; 101110001100110110000101001101111; 000011000011011010101000100000110; 100011000100010010001111100110110; 011000001001010001000000010010000; 001000000111001010000110010101111; 111111001110010010000111011000101; 010101010001101100101010111110110; 001101001111110111011010100010010; 110100100111100001001110001010110; 010001110111001110101101001010010; 000011100100011010000111011000100; 010100000101000000010010010101010; 111110001101101010100000001010100; 111100000000111010110000011011110; 101010000010011101011001100010110; 111001011110011110001000110011110; 111100000010010010000010100110000; 011011001010110001100110010010011; 011110111011101110101010010011110; 010011100110000010000101101001101; 010011101100110001101000001110010; 011010110100111110111000110001010; 111100101101100110000001110010010; 111010111100010001000000000101000; 000101011110111100011110000000010; 100000001001101110001110110001100; 000000010100001101100011111010010; 010111110110001001101100011010010; 001101000101111010110010010011010; 101110000001010000000100100010110; 001010000000110010100101101100111; 100000010111100010100010100011010; 000101110000010110111001010011010; 011110001011010010000001110000111; 101000010001000110101110111010111; 000000011100011001100101010100010; 011000010111111010011110110110010; 001111101011101010100110101000000; 110100110111110011100000011011010; 010111110101000010100101110110101; 011101100110010000011111000001110; 111100001101111100001101010111010; 000000000001110010001101101001110; 000011111100100101001100111100111; 011010101111110110000011000001110; 110000110010011010000100010001110; 100110101011010000011100011010010; 110010001110001010001111111011010; 111100011011101110100101010011111; 110001010111101001000100000011110; 111010001001101100111111111101010; 111010101011101010100011110100100; 111001111011010001010110111000010];
mstr = arrayfun(@(x) sprintf('%033u', x), m, 'UniformOutput', false); mbin = cell2mat(cellfun(@(s) s - '0', mstr, 'UniformOutput', false))
D = pdist(mbin,'minkowski',1); D_matrix = squareform(D); Z = [0; diag(D_matrix,1)]
fileID = fopen('c33HD.txt','w') fprintf(fileID,'%d\r\n',Z) fclose(fileID)
X= sum (Z)
fileID = fopen('c33sum.txt','w')
fprintf(fileID,'%d\r\n',X);
fclose(fileID);
David Young
David Young 2014년 12월 16일
편집: David Young 2014년 12월 16일
OK, I think the only way forward is to change the file containing the program, which includes the input data. This can be done manually or automatically. I can think of three possibilities:
  1. I guess you didn't type in all those numbers by hand. How were they generated? It might be easiest to change the program that generated them so that it inserts spaces.
  2. Otherwise, if you only have a few sets of numbers to change, it might be quickest for you to edit the program by hand, inserting single quotes round each 33-digit number. You can do this pretty quickly with the editor's find-and-replace, replacing < semicolon><space> with < quote><semicolon><space><quote> to do all except the first and last. This means the data will be read as strings and Guillaume's technique for inserting spaces will work and you can move on.
  3. If you have many sets of data that all need changing, it would be possible to write a program to make the edits instead of doing it by hand. This isn't a big task but you might have to wait a little while for it.
Guillaume
Guillaume 2014년 12월 16일
편집: Guillaume 2014년 12월 16일
Sarah, unfortunately you cannot store binary numbers that big the way you have done (as double). The biggest integer you could store that way in matlab would have at most 15 digits. Yours have 33!
To prove it:
a = 110011100010101011100010101010111; %the first element of your m
b = 110011100010101011100010000000000; %a different number with the same beginning
isequal(a, b) %return 1, which means they are equal
b = a
As it is your m is unusable. You've already lost the true value of your numbers. Therefore as David said and as I suggested, you need to change the way you load / create these numbers in the first place. Either load / generate them as strings, or as decimal numbers, or as you want as a matrix of 0 and 1.
Without seeing the code that creates / loads m we can't really help you further. I suggest you start a new question for this.
kavitha sundu
kavitha sundu 2016년 10월 10일
Hey,Sarah. I have a similar problem .Did you by any chance found the answer??

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

카테고리

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

질문:

2014년 12월 14일

댓글:

2016년 10월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by