how to create loops to automatically select next bits??

1 vue (au cours des 30 derniers jours)
Ayesha Punjabi
Ayesha Punjabi le 4 Juin 2019
Modifié(e) : Jan le 5 Juin 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 commentaires
Ayesha Punjabi
Ayesha Punjabi le 4 Juin 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 le 5 Juin 2019
@Ayesha: By teh way, you can omit the "mat", because nobody else use this expression. Usually "mat" means a ".mat" file.

Connectez-vous pour commenter.

Réponses (3)

Jan
Jan le 4 Juin 2019
Modifié(e) : Jan le 4 Juin 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 commentaires
Ayesha Punjabi
Ayesha Punjabi le 4 Juin 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 le 5 Juin 2019
Modifié(e) : Jan le 5 Juin 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)

Connectez-vous pour commenter.


KALYAN ACHARJYA
KALYAN ACHARJYA le 4 Juin 2019
Modifié(e) : KALYAN ACHARJYA le 4 Juin 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 le 4 Juin 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, [])

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by