how to create bit vector depending on another bit vector ?

hi, I have 20 bits in a vector (binary bits): for example, start from:
TMP_reg = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
TMP_reg = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1];
TMP_reg = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1];
..
..
end at:
TMP_reg = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
I want to produce another bits vector that depends on TMP_reg.
The new vector will be 40 bits vector on condition like that:
  1. For every bit that equal to 1 in TMP_reg, the New_reg will be 1 0 (as long as TMP_reg bit is 1).
  2. For every bit that equal to 0 in TMP_reg the New_reg will be 0 1 (as long as TMP_reg bit is 0).
Any suggestion how to do it?
Thanks,
Henry

1 件のコメント

Steven Lord
Steven Lord 2016 年 3 月 2 日
It's not clear what you're looking for. Let's take smaller examples.
What do you want New_reg to be for TMP_reg equal to [1 0 1 1 0 0] and why?
What do you want New_reg to be for TMP_reg equal to [1 1 1 1 1 1] and why?
What do you want New_reg to be for TMP_reg equal to [0 0 0 0 0 0] and why?

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2016 年 3 月 2 日

1 投票

One possible way:
bitpatterns = {[0 1], [1 0]}; %in the order pattern for 0, pattern for 1
TMP_reg = [0 0 1 0 1 1 0]; %length does not matter
TMP_2 = cell2mat(bitpatterns(TMP_reg + 1)) %add 1 to TMP_reg to create indices 1 or 2 used to index bitpatterns

6 件のコメント

Henry Buck
Henry Buck 2016 年 3 月 3 日
It works.... :-))).
Thanks a lot.
Another issue is to creat TMP_reg. I have index input that runs from 0 to 20. In each stage of the index(runs from 0 to 20), I am changing TMP_reg manually: if index = 0 then TMP_reg=[0 0 0....0 0 1] if index = 1 then TMP_reg=[0 0 0....0 1 1] if index = 2 then TMP_reg=[0 0 0....1 1 1] .. .. if index = 19 then TMP_reg=[0 1 1....1 1 1] if index = 20 then TMP_reg=[1 1 1....1 1 1]
can I do it in better way ? I mean Automaticaly instead manually ?
Thanks a lot, Henry
Guillaume
Guillaume 2016 年 3 月 3 日
Well, yes of course, it can be done. Either:
for index = 1:20 %can't run from 0 to 20, that's 21 bits
TMP_reg = [zeros(20-index, 1), ones(index, 1)];
%do something with it
end
Or even simpler:
TMP_allreg = flipud(triu(ones(20)));
for index = 1:20
TMP_reg = TMP_allreg(index, :);
%... do something with it
end
Henry Buck
Henry Buck 2016 年 3 月 3 日
編集済み: Guillaume 2016 年 3 月 3 日
great,
One thing I dont understand.... From command prompt/line it works perfect, but when I write it into file.m it does not work. how come ?
function gate_h = gate_array(I_H, indx_h)
if(I_H > 0)
for indx_h=1:20
TMP_reg = [zeros(1, 20-indx_h), ones(1, indx_h)];
end
end
bitpatterns = {[0 1], [1 0]};
gate_h = cell2mat(bitpatterns(TMP_reg + 1));
the result is the same 40 bit output and does not change while the indx changing....
can you understand why ?
Henry Buck
Henry Buck 2016 年 3 月 3 日
編集済み: Guillaume 2016 年 3 月 3 日
hi,
I just change it in that way and it works.
But in simulink lots of error :
function gate_h = gate_array(I_H, indx_h)
no_cells = 20;
if(I_H > 0)
TMP_reg = [zeros(1, no_cells-indx_h), ones(1, indx_h)];
else
TMP_reg = [ones(1, indx_h), zeros(1, no_cells-indx_h)];
end
bitpatterns = {[0 1], [1 0]};
gate_h = cell2mat(bitpatterns(TMP_reg + 1));
for example:
Component: Simulink | Category: N.A.
Simulink does not have enough information to determine output sizes for
this block. If you think the errors below are inaccurate, try specifying
types for the block inputs and/or sizes for the block outputs.
Component: MATLAB Function | Category: Coder error
Cell arrays are not supported for code generation.
Function 'gate_array_h' (#130.645.659), line 20, column 15:
"{[0 1], [1 0]}"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
Undefined function or variable 'bitpatterns'. The first assignment to a local variable determines its class.
Function 'gate_array_h' (#130.679.690), line 21, column 19:
"bitpatterns"
Launch diagnostic report.
Component: MATLAB Function | Category: Coder error
Errors occurred during parsing of MATLAB function 'gate_array_h'(#129)
Component: MATLAB Function | Category: Coder error
Error due to multiple causes.
Errors occurred during parsing of MATLAB function 'gate_array_h'(#129)
Error in port widths or dimensions. Output port 1 of 'untitled/gate_array_h/indx_h' is a one dimensional vector with 1 elements.
Component: Simulink | Category: Model error
Do you know how to solve it ?
Thanks, Henry
Guillaume
Guillaume 2016 年 3 月 3 日
Why bother generating TMP_reg in your function if it's only a temporary variable? You may as well generate the final output directly:
function gate_h = gate_array(I_H, indx_h)
gate_h = [repmat([1 0], 1, indx_h), repmat([0 1], 1, 20-indx_h)];
if I_H > 0
gate_h = fliplr(gate_h);
end
end
This also avoids the cell array which appears to cause problem with Simulink.
I know nothing about Simulink, so no idea if it'll be happy with the above.
Henry Buck
Henry Buck 2016 年 3 月 3 日
Graet,
I will forward your answer(by your permition..) to someone who knows how to convert it into Simulink models, hopefully...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimulink Functions についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by