How to convert logical vector to a number?

조회 수: 22 (최근 30일)
Tor Hammarbäck
Tor Hammarbäck 2022년 4월 4일
댓글: Bharath Venkataraman 2022년 4월 5일
Say I have a logical vector:
logicVector = logical([1 0 0 0 1 0 0 0]);
How do one convert it to a number?
From the code above the conversion would give 136 assuming it is unsigned and MSB first. Preferably the type would be fixed point. Best would be a function similar to matlabs 'fi'.
address = logicVec2fi(logicVector, false, true, 0); % logicVec2fi(input, signed, MSB, fraction length)
This will be used in simulink for a binary search where I get a boolean from each search-step which forms the index/address of a ram from the HDL Coder toolbox

채택된 답변

Tor Hammarbäck
Tor Hammarbäck 2022년 4월 4일
Thank you for your solutions! I finally found that the bit concat block does what I want when using only one input port.
  댓글 수: 1
Bharath Venkataraman
Bharath Venkataraman 2022년 4월 5일
I'm glad you found the block. You can also this with the MATLAB function bitconcat.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

DGM
DGM 2022년 4월 4일
Well here's one way.
logicVector = logical([1 0 0 0 1 0 0 0]);
sum(2.^(numel(logicVector)-find(logicVector)))
ans = 136
I have no idea if that's the most appropriate for your application. I have no familiarity with using fi or Coder, so I'm just going to stop there.
  댓글 수: 1
Tor Hammarbäck
Tor Hammarbäck 2022년 4월 4일
Thank you for your soluiton! Perhaps this is the only way, but it seems to use unnessesarly heavy computations when a number is represented in binary after the HDL code generation anyways.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 HDL Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by