I have some values as : A=
0
0
1
0
1
0
1
0
0
1
1
0
I want to take every 4 values in a string.
Output should be : B =
0 0 1 0
1 0 1 0
0 1 1 0
And, after that I want to covert the values from binary to decimal.
Final output should be :
2
10
6
It cuould be better for me if it is done in a loop. Because, I have a length of "A" near 40+ lacs.

 채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 1월 4일
편집: KALYAN ACHARJYA 2021년 1월 4일

1 개 추천

B=reshape(A,[4,3])';
C=num2str(B);
bin2dec(C)
Output :
ans =
2
10
6
Note: Loop is not necessity .

댓글 수: 4

Noman Abir
Noman Abir 2021년 1월 4일
You used B=reshape(A,[4,3])'; where 3 is for length.
What can I use if the length is unidentified.? (Length can be anything anytime)
Should I then use this code..?
K = length(A);
B=reshape(A,[4,K])';
Stephen23
Stephen23 2021년 1월 4일
편집: Stephen23 2021년 1월 4일
"Should I then use this code..? K = length(A); B=reshape(A,[4,K])';"
No, because the reshaped vector would have to have K/4 columns, not K columns.
But in any case, experienced MATLAB users would recommend this syntax:
B = reshape(A,4,[]).';
and simply letting reshape calculate how many columns are required.
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 1월 4일
Thanks @Stephen
Noman Abir
Noman Abir 2021년 1월 4일
Thank You both @KALYAN & @Stephen.

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

추가 답변 (0개)

카테고리

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

질문:

2021년 1월 4일

댓글:

2021년 1월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by