how to add space between matrix elements?
23 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
David Young
on 14 Dec 2014
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 Comments
Guillaume
on 15 Dec 2014
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
on 15 Dec 2014
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?
More Answers (1)
Guillaume
on 14 Dec 2014
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 Comments
Guillaume
on 16 Dec 2014
Edited: Guillaume
on 16 Dec 2014
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
on 10 Oct 2016
Hey,Sarah. I have a similar problem .Did you by any chance found the answer??
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!