Hi,
I wonder how to make invert bit vector ?
*For example:*
I have A vector: A=[0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0] that mean 2 couples LSB are 1 0 1 0 and the rest are 01 0 1 0 1 0 1 0 1 0 1
And I want to invert it to B=[1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1] the result of the inverted vector should be MSB 1 0 1 0 and the rest couple bits 0 1 0 1 0 1 0 1 0 1 0 1.
Anyone knows how to do it ?
Thanks,
Henry

 Réponse acceptée

Stephen23
Stephen23 le 5 Mar 2016

0 votes

>> A=[0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0];
>> B=[1 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1];
>> C = [A(end-3:end),A(1:end-4)];
>> isequal(B,C)
ans =
1

6 commentaires

Henry Buck
Henry Buck le 5 Mar 2016
Modifié(e) : Stephen23 le 5 Mar 2016
Hi,
Vactor A changing all the time (depends on an input that runs from 0 to 10 for example. for example:
in=0 then A = [0 1 0 1 0 1....0 1 0 1]
in=1 then A = [0 1 0 1 0 1....0 1 1 0]
in=2 then A = [0 1 0 1 0 1....1 0 1 0]
...
in=9 then A = [0 1 1 0 1 0....1 0]
in=10 then A = [1 0 1 0 1 0....1 0]
the resolt should be:
A (when in=0) then B = [0 1 0 1 0 1....0 1 0 1]
A (when in=1) then B = [1 0 0 1 0 1....0 1 0 1]
A (when in=2) then B = [1 0 1 0 0 1....1 0 1 0]
and so on...
Can you know how to do it ?
Thanks, Henry
Stephen23
Stephen23 le 5 Mar 2016
@Henry Buck: today I formatted your code correctly for you, but in future you can do it yourself. It is pretty easy: select the code text and click on the {} Code button that you will find above the textbox.
It must be a slow day for, but your three examples do not clarify the pattern for me. Please provide some more complete examples. Simplifying the examples does not help either, because then I have to make up something to test my code with, and there is no guarantee that I will make up the same vectors as you have. So if you want a solution to work for your example then you need to provide them to us.
Henry Buck
Henry Buck le 5 Mar 2016
function gate_h = gate_array_h6(I_H, indx_h)
no_cells = 20; gate_h = zeros(1, no_cells * 2); gate_h(1:2:indx_h*2) = 0; gate_h(indx_h*2+1:2:no_cells*2-1) = 0;
if I_H > 0 gate_h(2:2:(no_cells-indx_h)*2) = 1; gate_h((no_cells-indx_h)*2+1:2:no_cells*2-1) = 1; else gate_h(1:2:indx_h*2-1) = 1; gate_h((indx_h+1)*2:2:no_cells*2) = 1; end
This code craete gate_h vector according to I_H and indx_h(goes from 0 to 20). the result of gate_h(depending on the inputs) is 40 bits: indx = 0 then gate_h = [0 1 0 1..... 0 1 0 1 0 1] indx = 1 then gate_h = [0 1 0 1..... 0 1 0 1 1 0] indx = 2 then gate_h = [0 1 0 1..... 0 1 1 0 1 0] and so on....till indx = 20: indx = 20 then gate_h = [ 1 0 1 0 1 0.... 1 0 1 0].
The code above works perfect.
  • On the same function I want to built new function gate_l.the result should be:*indx = 0 then gate_l = [0 1 0 1..... 0 1 0 1 0 1]indx = 1 then gate_l = [ 1 0 0 1..... 0 1 0 1 0 1]indx = 2 then gate_l = [ 1 0 1 0..... 0 1 0 1 0 1]indx = 2 then gate_l = [ 1 0 1 0 1 0..... 0 1 0 1]and so on....
I hope it is much more clarify. Thank you for your answer.
Henry
Henry Buck
Henry Buck le 5 Mar 2016
Thanks a lot.
Finally, it works... :-)))
Walter Roberson
Walter Roberson le 6 Mar 2016
Please format your entries.
Henry Buck
Henry Buck le 12 Mar 2016
Hi, Thank you for Your answer.
I think I did not understand your last comment.
Henry

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by