フィルターのクリア

How do you subtract/add with roll under/over

3 ビュー (過去 30 日間)
Matthew Doveton
Matthew Doveton 2013 年 8 月 19 日
When you subtract two uint numbers together example:
uint8(1) - uint8(2)
I would like the answer to be 255, instead no matter what the value MATLAB returns 0, likewise if you try adding:
uint8(255) + uint8(1)
MATLAB returns 255.
I have written my own add and subtract functions for this purpose which work fine but I am wondering whether there is actually something in built that I am missing. If there is could you please point me in the right direction.
Thanks.
  1 件のコメント
Matthew Doveton
Matthew Doveton 2013 年 8 月 19 日
The way I posted my question appears to have caused some confusion. I do not have a problem implementing the desired effect myself but would like to know if this is an option within MATLAB that I'm unaware of (fairly new to MATLAB)
for uint8(1) - uint8(2) = 255, is what I want.
I basically want it to mimic my microcontroller code. If I were to write:
a = 1 - 2;
a would == 255, if a was a byte. In MATLAB a == 0;
As stated above, I have written a function to replicate this behaviour but thought that MATLAB would have this as an option. I personally would like MATLAB to mimic what would happen within a microcontroller as I use MATLAB primarily to test code that I then would convert to C for use within a microcontroller. I had some code that had an issue with rolling under at a certain condition. I could not replicate this in MATLAB due to this issue.

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

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 19 日
編集済み: Azzi Abdelmalek 2013 年 8 月 19 日
Edit
a=uint8(-1);
out=uint8(255)*uint8(not(a))+a
  2 件のコメント
Matthew Doveton
Matthew Doveton 2013 年 8 月 19 日
for uint8(1) - uint8(2) = 255, is what I want.
I basically want it to mimic my microcontroller code. If I were to write:
a = 1 - 2;
a would == 255, if a was a byte. In MATLAB a == 0;
As stated above, I have written a function to replicate this behaviour but thought that MATLAB would have this as an option. I personally would like MATLAB to mimic what would happen within a microcontroller as I use MATLAB primarily to test code that I then would convert to C for use within a microcontroller. I had some code that had an issue with rolling under at a certain condition. I could not replicate this in MATLAB due to this issue.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 8 月 19 日
編集済み: Azzi Abdelmalek 2013 年 8 月 19 日
Look at edited answer

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


Sean de Wolski
Sean de Wolski 2013 年 8 月 19 日
編集済み: Sean de Wolski 2013 年 8 月 19 日
x = uint8(1);
y = uint8(2);
z = max(double(x)-double(y),0)+uint8(x < y).*255

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by