how to create loops to automatically select next bits??

2 次查看(过去 30 天)
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 个评论
Ayesha Punjabi
Ayesha Punjabi 2019-6-4
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 2019-6-5
@Ayesha: By teh way, you can omit the "mat", because nobody else use this expression. Usually "mat" means a ".mat" file.

请先登录,再进行评论。

回答(3 个)

Jan
Jan 2019-6-4
编辑:Jan 2019-6-4
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 个评论
Ayesha Punjabi
Ayesha Punjabi 2019-6-4
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 2019-6-5
编辑:Jan 2019-6-5
@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
KALYAN ACHARJYA 2019-6-4
编辑:KALYAN ACHARJYA 2019-6-4
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 2019-6-4
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, [])

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by