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)
채택된 답변
추가 답변 (1개)
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
sara
2014년 12월 14일
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
2014년 12월 15일
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.
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:
- 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.
- 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.
- 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.
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
2016년 10월 10일
Hey,Sarah. I have a similar problem .Did you by any chance found the answer??
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!