i have matrix =001001001111011101111;
and i want to convert it to
001
001
001
111
011
101
111
i know reshape function
matrix=reshape(matrix,[],3);
but it not get the answer which i need

 채택된 답변

David Hill
David Hill 2020년 5월 18일

1 개 추천

m =[0 0 1 0 0 1 0 0 1 1 1 1 0 1 1 1 0 1 1 1 1];
a=reshape(m,3,[])';

댓글 수: 5

Shehab Tarek
Shehab Tarek 2020년 5월 18일
note i want to convert it in 3 colum 7
Shehab Tarek
Shehab Tarek 2020년 5월 18일
i want to take all 3 bit and save it in 7rows
Stephen23
Stephen23 2020년 5월 18일
편집: Stephen23 2020년 5월 18일
This answer works, I don't see what the problem is:
>> m = [0,0,1,0,0,1,0,0,1,1,1,1,0,1,1,1,0,1,1,1,1];
>> a = reshape(m,3,[]).'
a =
0 0 1
0 0 1
0 0 1
1 1 1
0 1 1
1 0 1
1 1 1
Shehab Tarek
Shehab Tarek 2020년 5월 18일
good
but how to make the first line from double matrix to M=[0,0,1,......];
Your example:
M=[0,0,1,......];
is also a double matrix. Try it and see:
>> M=[0,0,1];
>> class(M)
ans = double
So instead of a double array you want a double array? In any case, reshape does not change the array type, so whatever type array you have at the start is what you will get at the end.

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

추가 답변 (1개)

Sai Bhargav Avula
Sai Bhargav Avula 2020년 5월 18일
편집: Sai Bhargav Avula 2020년 5월 18일

1 개 추천

Hi,
Try the following code
convert matrix to char
matrix ='001001001111011101111';
matrix=reshape(matrix,[],3);
Hope this helps!

댓글 수: 7

Shehab Tarek
Shehab Tarek 2020년 5월 18일
편집: Shehab Tarek 2020년 5월 18일
sir this is my output i want to focus in my answer i want it to be like
001 =row1
001 =row2
001 ..
111 ..
011 ..
101 ..
111 row7
take all three bit and save it in 7 row
I didnot understand,
if you need a 7*1 array, try this code and operate over the result
matrix ='001001001111011101111';
matrix=string(reshape(matrix,[],3));
This results in a 7*1 string array and can operate over them like
bin2dec(matrix(3));
Stephen23
Stephen23 2020년 5월 18일
"i want to know why you convert the double matrix to string "
Because Sai Bhargav Avula:
  1. did not actually solve the question you asked.
  2. did not actually check their output against your expected output.
  3. used a pointless data type conversion that does not actually solve either of 1. or 2.
Sai Bhargav Avula
Sai Bhargav Avula 2020년 5월 18일
편집: Sai Bhargav Avula 2020년 5월 18일
From the question
I understood that the input is
matrix= 001001001111011101111;
and not an array.
and from the comment to the other answer I understood that the expected output to be 7*1 martix and not a 7*3 matrix, with each row to be 3 bit value created from matrix variable.
Stephen23
Stephen23 2020년 5월 18일
편집: Stephen23 2020년 5월 18일
"I understood that the input is..."
If the input is either:
  • a character vector of '0' and '1' characters, or
  • a numeric vector of 0 and 1 values,
then exactly the same operations (reshape and transpose) can be applied to get the requested order.
"I understood that the expected output to be 7*1 martix and not a 7*3 matrix"
This is certainly possible, but does not resolve that your code still put the wrong input elements in the wrong locations. Converting it to a 7x1 string array does not fix that.
>> matrix ='001001001111011101111';
>> matrix=reshape(matrix,[],3) % your code
matrix =
001
011
110
011
011
101
011
Lets compare your output against the requested output given in the original question:
your: original:
001 001
011 001
110 001
011 111
011 011
101 101
011 111
Converting to string will not fix that your code put the incorrect elements in each row.
Discussion of input class and output type conversion are simply distractions from that.
Shehab Tarek
Shehab Tarek 2020년 5월 18일
편집: Shehab Tarek 2020년 5월 18일
thanks in advance sir
Sai Bhargav Avula
Sai Bhargav Avula 2020년 5월 18일
편집: Sai Bhargav Avula 2020년 5월 18일
Thanks for the explanation and clearing my confusion Stephen Cobeldick,
I was thinking more about matrix being a single value(matrix = 001001001111011101111) and not an array( [0 0 1 .....]) and output to be a single colum vector (7*1) and overlooked the expected result .
The answer should have been like what David answered
matrix ='001001001111011101111';
matrix= string(reshape(matrix,3,[])');

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2020년 5월 18일

편집:

2020년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by