# Answer to Question #62142 in MatLAB | Mathematica | MathCAD | Maple for Linda

Question #62142

Write a matlab function nybblise which takes as input digital signal of 0’s and 1’s stored in a vector of size N ×1, and breaks it into four 4-bit nybbles, which are returned as the columns of a 4×N/4 matrix.

Your function must return -1 if the N is not a multiple of 4.

Following is the signature of the function.

function A = nybblise(d)

Input: d - digital signal as a vector of size N x 1

Output: A - nybbles as a 4 x N/4 matrix OR -1 if N is not a multiple of 4

Note: This function must use loops rather than MATLAB built-in functions such as reshape, fliplr, flipud.

Your function must return -1 if the N is not a multiple of 4.

Following is the signature of the function.

function A = nybblise(d)

Input: d - digital signal as a vector of size N x 1

Output: A - nybbles as a 4 x N/4 matrix OR -1 if N is not a multiple of 4

Note: This function must use loops rather than MATLAB built-in functions such as reshape, fliplr, flipud.

Expert's answer

% nybblise.m

function A = nybblise(d)

% First index is used to calculate how many multiples of 4 (thus number of columns) it takes to get

% 'd' given 'd' is a multiple of 4 to begin with.

index = 0;

% Second index is used to for a loop where a zero matrix of size 4*N (where

% N = number of columns needed which is the value of 'index') in which the

% values of 'd' replace the zero matrix to output A with a 4*N matrix with the values of 'd'.

index2 = 0;

% d_mul is first set to the length of input 'd' and is used later to find

% the number of columns needed for our output using 'index'

d_mul = length(d);

%

while d_mul > 0

d_mul = d_mul - 4;

index = index + 1;

end

matrix = zeros(4,index);

if d_mul == 0

while index2 <= (index-1)

matrix(:,(index2+1)) = d([(index2*4+1) (index2*4+2) (index2*4+3) (index2*4+4)]);

index2 = index2+1;

end

A = matrix;

else

A = -1;

end

end

function A = nybblise(d)

% First index is used to calculate how many multiples of 4 (thus number of columns) it takes to get

% 'd' given 'd' is a multiple of 4 to begin with.

index = 0;

% Second index is used to for a loop where a zero matrix of size 4*N (where

% N = number of columns needed which is the value of 'index') in which the

% values of 'd' replace the zero matrix to output A with a 4*N matrix with the values of 'd'.

index2 = 0;

% d_mul is first set to the length of input 'd' and is used later to find

% the number of columns needed for our output using 'index'

d_mul = length(d);

%

while d_mul > 0

d_mul = d_mul - 4;

index = index + 1;

end

matrix = zeros(4,index);

if d_mul == 0

while index2 <= (index-1)

matrix(:,(index2+1)) = d([(index2*4+1) (index2*4+2) (index2*4+3) (index2*4+4)]);

index2 = index2+1;

end

A = matrix;

else

A = -1;

end

end

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment