Setting bounds on a calculated array

2 ビュー (過去 30 日間)
Alex Spoor
Alex Spoor 2018 年 3 月 29 日
回答済み: David Fletcher 2018 年 3 月 30 日
I have a program that will randomly generate a number [-1 1] and add it to a random position in a vector. The program is supposed to work its way to an integer that corresponds with the ASCII digit so when converted it displays a target phrase.
The problem is that the vector in question is being displayed as characters and I need to limit it to numbers between 1:127. I discovered that when left to calculate it ends up in the several thousands both positive and negative.
  1 件のコメント
Jos (10584)
Jos (10584) 2018 年 3 月 30 日
This is all quite vague ... What have you tried so far? Did you write some (pseudo-) code or flowchart to start with? What is the expected input and expected output of this "program", in proper matlab variables?

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

回答 (1 件)

David Fletcher
David Fletcher 2018 年 3 月 30 日
It's easy enough to index the places where the values in the vector are greater than 127 or less than 1
indices=charVector<0 | charVector>127
You need to then decide what to do with the values that are outside the bounds - if you want to set the values to the (same) in-range random number you can use
a(indices)=randi(127)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by