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

1 view (last 30 days)
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 Comment
Rik
Rik on 18 Jun 2021
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.

Sign in to comment.

Accepted Answer

Rik
Rik on 6 Apr 2021
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 Comments
Rik
Rik on 14 Apr 2021
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!