Creating [-1 1] matrix given length n

조회 수: 3 (최근 30일)
Alice Zurock
Alice Zurock 2020년 3월 22일
편집: Ameer Hamza 2020년 3월 24일
Hi,
I want to create N length long [-1 1] matrix,
For example: Length=2
Output:
[ 1 1
1 -1
-1 1
-1 -1]
Length = 3
Output:
[ 1 1 1
1 1 -1
1 -1 1
-1 1 1
......
-1 -1 -1]
And so on, How can do that?

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 3월 22일
Hi Alice,
You can try the following:
len = 2;
N = 2^len;
bi = de2bi(N-1:-1:0); % Convert decimal numbers in to binary
bi(bi==0) = -1; % Replace the values of 0 with -1
Hope this helps.
Regards,
Sriram
  댓글 수: 1
Sriram Tadavarty
Sriram Tadavarty 2020년 3월 22일
Hi Alice,
If the answer is helpful for the question asked, do accept the answer. I suggest you to start a new question for this. Anyhow i can provide some insights of how this can be done here.
To perform the circular shift, you can use circshift function. To iterate the same for multiple times, use a for loop.
For example, for row selected
row = [1 -1 1];
crow = circshift(row,1); % This will provide the value of [1 1 -1]
% Now perform the element wise matrix multiplication and add them
out = sum(row.*crow); % This will give you the -1
% Based on out, display the string accordingly
Hope this helps.
Regards,
Sriram

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

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 3월 22일
편집: Ameer Hamza 2020년 3월 22일
See combvec():
Length = 3;
x = mat2cell(repmat([1 -1], Length, 1), ones(Length,1), 2);
out = combvec(x{:})';

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by