필터 지우기
필터 지우기

Creating a logical array

조회 수: 512 (최근 30일)
E K
E K 2012년 8월 4일
댓글: Image Analyst 2023년 1월 16일
hi guys,
How can i create a logical array [1 0 1 0 1 1 ....] 1-by-15 it will go into gamultiobj with bitstring so the the arranging is not important.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 4일
x=boolean([0 1 0 1 0 1 0 1])
  댓글 수: 1
Image Analyst
Image Analyst 2023년 1월 16일
Note: the boolean function is only in the Stateflow toolbox which is kind of rare. See the warning in the help:
The operator boolean is supported only in Stateflow® charts. In MATLAB®, use logical.
If you don't have the Stateflow toolbox, use logical as shown in the other answers below. Actually, even if you do have it, I think you should use logical instead, especially for portability of your code.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2012년 8월 4일
편집: Image Analyst 2023년 1월 16일
To get a random placement of trues and falses, use randi
logicalArray = logical(randi(2, [1 15]) - 1)
logicalArray = 1×15 logical array
1 1 1 1 0 0 0 1 1 0 1 0 0 0 1
Otherwise you can put in exactly what you want:
logicalArray = logical([1, 0, 1, 0, 1, 1])
logicalArray = 1×6 logical array
1 0 1 0 1 1

Captain Karnage
Captain Karnage 2023년 1월 16일
What's the ultimate goal? If you need to initialize a logical array, you can use true or false:
either
logicalArray = false(1,15);
-OR-
logicalArray = true(1,15);
will initialize a 1x15 logical array that you can then set the individual values for, then if you set any element, like
logicalArray(5) = 1;
It will be of type logical rather than double.
If you already know your entire array, you can also do it manually with true and false:
logicalArray = [ true false true false true true true false true false true true true false true ];
will output
1 x 15 logical array
[ 1 0 1 0 1 1 1 0 1 0 1 1 1 0 1]

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by