how to create loops to automatically select next bits??
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Jan
el 5 de Jun. de 2019
@Ayesha: By teh way, you can omit the "mat", because nobody else use this expression. Usually "mat" means a ".mat" file.
Respuestas (3)
Jan
el 4 de Jun. de 2019
Editada: Jan
el 4 de Jun. de 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 comentarios
Jan
el 5 de Jun. de 2019
Editada: Jan
el 5 de Jun. de 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)
KALYAN ACHARJYA
el 4 de Jun. de 2019
Editada: KALYAN ACHARJYA
el 4 de Jun. de 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
0 comentarios
Guillaume
el 4 de Jun. de 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, [])
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!