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

1 次查看(过去 30 天)
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 个评论
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