Need matrix or vector with 1,0,1,0,1,0.........
조회 수: 7 (최근 30일)
이전 댓글 표시
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 :)
댓글 수: 0
채택된 답변
John D'Errico
2021년 10월 12일
편집: John D'Errico
2021년 10월 12일
Hint: Use mod.
For example,
mod(1:25,2)
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.
댓글 수: 0
추가 답변 (2개)
Jan
2021년 10월 12일
Do you now the colon operator already?
a = 2;
b = 15;
a:b
a:3:b
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
댓글 수: 0
Mustafa
2025년 5월 10일
A = ones(n); % creates [n x n] - matrix
a = reshape(A,1,25); % creates a [1 x n^2] - matrix
As I understand you want the output in the double-format. I want to show this in logical-format too. What is about:
n = 5;
1) a1 = repmat([0,1],1,n^2); % double-format
2) a2 = logical(a1); % logical-format
3) a3 = repmat([false,true],1,n^2); % logical-format
The results of a2 and a3 should be the same. a2 converts a1 from double into logical but a3 should be faster than a2.
If you handle big matrices (large n): I don't know the efficiency of the built-in-function named "repmat()".
Greetings,
Mustafa
댓글 수: 1
Stephen23
2025년 5월 11일
n = 6e7;
timeit(@() logical(repmat([0,1],1,n)))
timeit(@() repmat([false,true],1,n))
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!