modify a program to obtain the same result from right to left

조회 수: 1 (최근 30일)
high speed
high speed 2021년 4월 6일
댓글: Rik 2021년 6월 18일
I have this matrix:
H=[0 1 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 1]
I want use but from right to left. It means I want obtain this matrix
H=[0 1 0 0 0 0 0 0 0 0 1 0;
0 0 0 1 0 0 0 0 1 0 0 0;
0 0 0 0 0 1 1 0 0 0 0 0;
0 0 0 0 1 0 0 1 0 0 0 0;
0 0 1 0 0 0 0 0 0 1 0 0;
1 0 0 0 0 0 0 0 0 0 0 1]
  댓글 수: 1
Rik
Rik 2021년 6월 18일
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is considered rude.
modify a program to obtain the same result from right to left
I have this MATLAB program
dr = 6; dc = 3; N = 12; M=6; r=gcd(M,N); c=N/r; b=M/r;
H=zeros(M,N);
%Full rank (left)
vCol = c:c:N;
for iCol = 1:numel(vCol )
iRow = (iCol - 1) * b + 1;
H(iRow:iRow + b - 1, vCol(iCol)) = 1;
end
Using this program I obtain the result of this matrix:
H=[0 1 0 0 0 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 0 0 0 1 0 0;
0 0 0 0 0 0 0 0 0 0 0 1]
I want use the same part of Full rank in this program but from right to left. It means I want obtain this matrix
H=[0 1 0 0 0 0 0 0 0 0 1 0;
0 0 0 1 0 0 0 0 1 0 0 0;
0 0 0 0 0 1 1 0 0 0 0 0;
0 0 0 0 1 0 0 1 0 0 0 0;
0 0 1 0 0 0 0 0 0 1 0 0;
1 0 0 0 0 0 0 0 0 0 0 1]
Notice: It's not an identity matrix because it depends on the rate b and c.

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

채택된 답변

Rik
Rik 2021년 4월 6일
Your loop can probably be replaced by a vectorized operation, but I will leave that for you to figure out. I suspect fliplr and or() are all you need:
dr = 6; dc = 3; N = 12; M=6; r=gcd(M,N); c=N/r; b=M/r;
H=zeros(M,N);
vCol = c:c:N;
for iCol = 1:numel(vCol )
iRow = (iCol - 1) * b + 1;
H(iRow:iRow + b - 1, vCol(iCol)) = 1;
end
H = double( H | fliplr(H) );
disp(H)
0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1
  댓글 수: 2
high speed
high speed 2021년 4월 6일
What do you mean by vectorized operation please
Rik
Rik 2021년 4월 14일
By vectorized opeation I mean something like this:
M=6;N=12;
H=zeros(M,N);
H( (M+1):(2*M+1):end )=1;
disp(H)
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1
This way you generate the entire array at once. Careful construction of the vector of indices might avoid a loop. With the exceptions of functions that hide a loop (e.g. cellfun and arrayfun), code without a loop will almost always be faster.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by