how to add space between matrix elements?

25 ビュー (過去 30 日間)
sara
sara 2014 年 12 月 14 日
コメント済み: kavitha sundu 2016 年 10 月 10 日
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 日
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 件のコメント
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 日
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 件のコメント
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??

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by