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

1 Ansicht (letzte 30 Tage)
high speed
high speed am 6 Apr. 2021
Kommentiert: Rik am 18 Jun. 2021
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 Kommentar
Rik
Rik am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 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 Kommentare
high speed
high speed am 6 Apr. 2021
What do you mean by vectorized operation please
Rik
Rik am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by