Need matrix or vector with 1,0,1,0,1,0.........

26 ビュー (過去 30 日間)
Lubos Plechac
Lubos Plechac 2021 年 10 月 12 日
回答済み: Jan 2021 年 10 月 12 日
clc;clear
n = 5
A = ones(n)
a = reshape(A,1,25) % I reshape matrix to vector I think that is easy to understand.
I dont know what to do as next step to make vector or matrix with 1 and 0. Give me please more ways how to do that. Iam begginer of programming and if you could give me advice how to start with programming , what could help me to understand syntax and ways of programming and more things about programming I would be grateful. Thanks all :)

採用された回答

John D'Errico
John D'Errico 2021 年 10 月 12 日
編集済み: John D'Errico 2021 年 10 月 12 日
Hint: Use mod.
For example,
mod(1:25,2)
ans = 1×25
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
But really, you need to start by looking at the Onramp tutorials, or read any text. I looked on YouTube once, and I found many beginning courses in MATLAB. When trying to learn any tool, it is a good idea to read the manuals.

その他の回答 (1 件)

Jan
Jan 2021 年 10 月 12 日
Do you now the colon operator already?
a = 2;
b = 15;
a:b
ans = 1×14
2 3 4 5 6 7 8 9 10 11 12 13 14 15
a:3:b
ans = 1×5
2 5 8 11 14
You can use this to create an index of the values, you want to set to 1 (or to zero):
x = ones(1, 10);
x(1:2:10) = 0
x = 1×10
0 1 0 1 0 1 0 1 0 1

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT-Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by