필터 지우기
필터 지우기

How to replace multiple rows of a matrix by rows of two other different matrix using a loop?

조회 수: 4 (최근 30일)
Hey guys, I am new to MATLAB, so my question might be a basic one, however I will post it anyway. I am creating a 100 by 20 matrix called X. I want to replace the first row of matrix X by the first row of matrix A (which is itself a 50 by 20 matrix). Similarly I would like to replace the second row of matrix X by the first row of matrix B (which is also a 50 by 20 matrix). Again rplacing third row of X by 2nd row of A, and 4th row of X by 2nd row of B and so on.
Let me explain it by an example.
x = zeros(4,2)
a =[2 3; 3 4]
b = [4 5; 7 8]
x(1,:)= a(1,:)
x(2,:)= b(1,:)
x(3,:)= a(2,:)
x(4,:)= b(2,:)
out: x = [ 2 3
4 5
3 4
7 8]
I can't do this process for a 100 by 20 matrix. Please is there any other way to do it while using loop. Thank you
  댓글 수: 2
DGM
DGM 2021년 3월 23일
If you're willing to wait a few minutes, I have a function in a pending toolbox update that does this exact thing. I can go ahead and make the update so you can use it.

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

답변 (1개)

DGM
DGM 2021년 3월 23일
편집: DGM 2021년 3월 23일
The MIMT toolbox here:
contains two functions interleave() and deinterleave(). See help interleave. The intent with these is to operate on images, but they'll work with any arrays up to 4D, so long as dim1 and dim2 are the same for all arrays.
A=ones(10);
B=ones(10)*2;
C=ones(10)*3;
stripewidth=1;
X=interleave(1,A,B,C,stripewidth);
Here, we have three arrays of constant value (1, 2, and 3). The first argument specifies the direction we're operating on (dim1, i.e. we're swapping rows). The stripewidth parameter specifies how many rows to swap at once (only 1). The result is:
X =
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
The function deinterleave() can be used to undo the operation and get three arrays back out.
If you instead want to pick row 1 from A, row 2 from B, row 3 from C, row 4 from A, etc, then the functions alternate() and dealternate() would do that.
If you would prefer to know how it's done, just open the function file and take a look. It boils down to this:
% allocate output array of calculated size
outpict=zeros(sout,inclass);
% idx is the comma-separated list used for output indexing
% it represents the expression inside the parentheses like so:
% outarray(subscriptvector,:)=inarray
idx=repmat({':'},[1 numel(sa)]);
% the input arrays are all held in the cell array imagepile
% loop through imagepile, delivering all the rows of each image
% to the rows of outpict selected by the subscript vector in idx{dim}
% dim is the dimension we're operating on
% width is stripewidth
x=1:sout(dim);
for g=1:N
idx{dim}=mod((x-1-(g-1)*width)/N,width)<(width/N-(1E-6));
outpict(idx{:})=imagepile{g};
end

카테고리

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