how to create loops to automatically select next bits??

2 Ansichten (letzte 30 Tage)
Ayesha Punjabi
Ayesha Punjabi am 4 Jun. 2019
Bearbeitet: Jan am 5 Jun. 2019
a = [1 0 1 0 1 1 1 1 1 0 0 0 0 0......] <--- 1 row 80 columns
I am trying to create a loop such that mat b has 1st 16 bits of mat a in first run and in next run the next 16 bits of mat a
please help me in how to write a code for such loop
should i use for loop or while loop
Kindly help
  3 Kommentare
Ayesha Punjabi
Ayesha Punjabi am 4 Jun. 2019
a = [1 0 1 0 1 0 0 1 0 1 0.... so mat a is 1 row and 80 columns of binary bits
in mat b i want to put 1st 16 bits of data from mat a and perform a mutiplication with another matrix which is 16 bits of binary data example c [1 1 1 1 0 0 0 0 1 0 1 0 0 0 0 0]
this was in first run
in next run I want to again put next 16 bits from mat a to mat b. use mat b to multiply with mat c
continue this process untill all the 80 bits are performed with multiplication with mat c but 16 at one time
Jan
Jan am 5 Jun. 2019
@Ayesha: By teh way, you can omit the "mat", because nobody else use this expression. Usually "mat" means a ".mat" file.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Jan
Jan am 4 Jun. 2019
Bearbeitet: Jan am 4 Jun. 2019
a = randi([0,1], 1, 80);
for k = 1:16:80
b = a(k:k+15);
...
end
Or:
aa = reshape(a, 16, 5);
for k = 1:5
b = aa(:, k).';
...
end
Or:
for k = 1:5
b = a((1 + (k-1) * 16):(k * 16));
...
end
  3 Kommentare
Ayesha Punjabi
Ayesha Punjabi am 4 Jun. 2019
thank you for your suggestions.
could you please let me know the . . . in the code. I tried running the code but it is showing just 80 as the output
Jan
Jan am 5 Jun. 2019
Bearbeitet: Jan am 5 Jun. 2019
@Ayesha: My code should not show anything as output. The "..." is the correct location to insert, what you want to be computed. You did not mention, what you want to happen with the 16 bit blocks of the vector. I've showed you some methods to obatin the 16 bit block in the variable b, but now it is your turn to insert the calculations with b.
Now you have mentioned, that you want to multiply b with the vector c - an elementwise or matrix multiplication?
c = [1 1 1 1 0 0 0 0 1 0 1 0 0 0 0 0];
aa = reshape(a, 16, 5);
result = zeros(1, 5);
for k = 1:5
b = aa(:, k);
result(k) = c * b; % Matrix multiplication ==> scalar output
end
But as said by Guillaume already: This works much nicer without a loop:
result = c * reshape(a, 16, 5)

Melden Sie sich an, um zu kommentieren.


KALYAN ACHARJYA
KALYAN ACHARJYA am 4 Jun. 2019
Bearbeitet: KALYAN ACHARJYA am 4 Jun. 2019
x=randi([0 1],1,80); %Here I choosed random, consider your actual a
c=[1 1 1 1 0 0 0 0 1 0 1 0 0 0 0 0]; %As given by you
for i=1:16:length(x)-15;
mat_b=x(i:i+15).*c;
fprintf('\n The start bit position: %d, mat b result:',i);
disp(mat_b);
end

Guillaume
Guillaume am 4 Jun. 2019
in mat b i want to put 1st 16 bits of data from mat a and perform a mutiplication with another matrix which is 16 bits of binary in next run I want to again put next 16 bits from mat a to mat b. use mat b to multiply with mat c continue this process untill all the 80 bits
As I expected for this you don't need a loop. Simply reshape your a in columns of 16 bit. And multiply at once all the columns with the same c:
a = randi([0 1], 1, 80); %row vector of 80 bits. demo data
c = [1 1 1 1 0 0 0 0 1 0 1 0 0 0 0 0]; %row vector
result = reshape(a, 16, []) .* c(:);
Each column of result correspond to a group of 16 bit. If you want it back as a 1x80 vector:
reshape(result, 1, [])

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by