am using this code to generate binary matrix dec2bin(rand(50,1)*2^32) but i need to add one space between the matrix element so i can use it for other commands. the output matrix needs space to be useful for other calculation. how to do this?

 채택된 답변

Guillaume
Guillaume 2014년 11월 9일
편집: Guillaume 2014년 11월 9일

1 개 추천

The following would work:
m = dec2bin(rand(50,1)*2^32)
mwithspaces = cell2mat(regexprep(num2cell(m, 2),'\d(?=\d)', '$0 '));
I have to ask though, why do you want to do that? It seems like an unnecessary step. I would think that any algorithm that follows is probably going to remove these spaces one way or another.
edit: was missing a bracket

댓글 수: 5

Image Analyst
Image Analyst 2014년 11월 9일
It gives a 2D character array, not a simple string. I think maybe he wants all those rows concatenated together with a space in between them.
In that case:
mwithspaces = regexprep(reshape(m', 1, []), '\d(?=\d)', '$0 '));
or
strjoin(num2cell(reshape(m', 1, [])), ' ');
But I still think that not doing this and changing the steps that follow to not require the spaces is probably a better idea.
Image Analyst
Image Analyst 2014년 11월 9일
I agree with you there.
As commented on Jan's answer, inserting spaces is a red herring. To convert a string of zeros and ones into a matrix of zeros and ones numbers, simply subtract '0' from the string.
This may be what you're looking for:
M = dec2bin(rand(10, 1) * 2^32);
M = M - '0'; %convert M into an array of numbers 0 or 1
[m, n] = size(M);
M2 = [zeros(m, 3*ceil(n/3)-n), M];
X = xor(xor(M2(:, 1:3:end-2), M2(:, 2:3:end-1)), M2(:, 3:3:end))
janny
janny 2014년 11월 10일
it works fine... thanks a lot....

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

추가 답변 (2개)

Jan
Jan 2014년 11월 9일
편집: Jan 2014년 11월 9일

1 개 추천

s = dec2bin(rand(50,1)*2^32);
t = repmat(' ', size(s, 1), size(s, 2) * 2 - 1);
t(:, 1:2:end) = s;

댓글 수: 2

janny
janny 2014년 11월 9일
편집: janny 2014년 11월 9일
i'm writing an algorithm to calculate CA rule 150 from this sequence, so when i try these codes didn't give the right answer, this is my code:
M = dec2bin(rand(10,1)*2^32);
[m,n] = size(t);
M2 = [zeros(m,3*ceil(n/3)-n),t];
X = xor(xor(M2(:,1:3:end-2),M2(:,2:3:end-1)),M2(:,3:3:end))
if : M = [0 0 0 1 1 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 1 0 0 1 0 1 1 1]
then X = 0 0 1 0 0 1 0 0 1 1 1
so how to make it run?
Guillaume
Guillaume 2014년 11월 9일
편집: Guillaume 2014년 11월 9일
I don't know what CA rule 150 is so why don't you explain it.
It is now clear however, that your original question is a case of xy problem. Looking at your attempted code, you don't need to insert spaces and actually want to operate on numbers rather than strings.
xor certainly does not work with strings.
See comment on my own answer for a possible solution.

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

yonatan gerufi
yonatan gerufi 2014년 11월 9일

0 개 추천

see in the "file exchange" :

댓글 수: 3

janny
janny 2014년 11월 9일
it did not run for this issue.if i have matrix as (1110) i want to add one space between the element of it (1 1 1 0) so i want to modify this code if its possible
dec2bin(rand(50,1)*2^32)
so it can add the space automatically.
yonatan gerufi
yonatan gerufi 2014년 11월 9일
perhaps i misunderstood you.
the code : dec2bin(rand(50,1)*2^32)
gives you string of ones & zeros. you can pad it with ' ' (space), and then you will have space between the elements.
Image Analyst
Image Analyst 2014년 11월 9일
It gives a 2D character array, not a simple string. I think maybe he wants all those rows concatenated together with a space in between them.

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

카테고리

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

질문:

2014년 11월 9일

댓글:

2014년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by