Simple Question Regarding Matlab

Status
Not open for further replies.

moonnightingale

Full Member level 6
Joined
Sep 17, 2009
Messages
362
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
3,832
I have two quesions

1. I have generated 1000 Random integers by this command.

Digits=randint(1,1000)

Now i want to save these bits in a group of 4 each. I mean first four bit in a array(1)
then next four in array (2) and so on

I know for loop will be used but i am not getting how to implement it


2. I have made code in matlab which involves binary digits 1 and 0
I want to perfom certaiin operations on this code

I want that Matlab should consider these 1 and 0s as binary and not decimal
 

Code:
clc
clear all
close all
Digits=randint(1,1000);
for i=1:4:length(Digits)
    k=1;
    for j=1:4       
        Dig_4(k,j) = Digits(i+j-1)
    end
    k=k+1;
end
say thankx by pressing Helped me
 

Thanks but output is not correct at start i am apsting it

In the start there is 1 then 2 then 3 and then 4 and then it continues.I want to have four bits right from the first.

Secondly i want to save it in an array of unique name like first can be

dig_41= 1 1 1 0

dig_42 = 0 0 0 1


and so on. I hope u got me. I want to use these unique names lateron. right now dig_4 is having the value which it got at end



Dig_4 =

1


Dig_4 =

1 1


Dig_4 =

1 1 1


Dig_4 =

1 1 1 0


Dig_4 =

1 1 1 0


Dig_4 =

1 0 1 0
 

Code:
clc
clear all
close all
Digits=randint(1,1000);
k=1;
for i=1:4:1000
    for j=1:4
        Dig_4(k,j) = Digits(i+j-1);        
    end 
    k=k+1;
end

for index=1:k-1
    Dig_4(index,:)
end

if u want to save it with unique names then use

a = Dig_4(1,: )
b = Dig_4(2,: )
.
.
.
 

Digits=randint(1,1000)
j=1;
array = zeros(250,4);

for i=1:4:size(Digits,2)
array(j, = Digits(i:i+3);
j = j + 1;
end

This code will create the array you mentioned in your first message.

To make MATLAB consider your values as binary values, you can convert your array to a logical array before using it.

x = logical(array);

In most cases this is unnecessary. What kind of operations you are talking about?

And for the naming convention you want, there isn't any fast way to do it. You have to create variables one by one as shown in the message above.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…