how to add space between matrix elements?

23 vues (au cours des 30 derniers jours)
sara
sara le 14 Déc 2014
Commenté : kavitha sundu le 10 Oct 2016
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 commentaires
Oleg Komarov
Oleg Komarov le 14 Déc 2014
What do you mean by adding space?
Jan
Jan le 14 Déc 2014
Modifié(e) : Jan le 14 Déc 2014
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?

Connectez-vous pour commenter.

Réponse acceptée

David Young
David Young le 14 Déc 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 commentaires
Guillaume
Guillaume le 15 Déc 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
David Young le 15 Déc 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?

Connectez-vous pour commenter.

Plus de réponses (1)

Guillaume
Guillaume le 14 Déc 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 commentaires
Guillaume
Guillaume le 16 Déc 2014
Modifié(e) : Guillaume le 16 Déc 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
kavitha sundu le 10 Oct 2016
Hey,Sarah. I have a similar problem .Did you by any chance found the answer??

Connectez-vous pour commenter.

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by