필터 지우기
필터 지우기

matrix generation without a for loop

조회 수: 2 (최근 30일)
Azza
Azza 2012년 10월 18일
Hi,
I am trying to build a matrix by giving each array in the matrix the same values. My original code looks like this:
for Y = 1:length(yv)
for X = 1:length(xv)
counter = 1;
M(:,counter,X,Y)= [0;0;1];
counter = counter;
M(:,counter,X,Y) = A*Rflip*M(:,1,X,Y)+B;
end
end
I was wondering if there was an efficient way to provide the same values for each array in the matrix the same values without using the for loop that is because my original matrix is very large. The code should use a counter as shown above. I have replicated the matrix for A, Rflip and B in order to accomodate the M value for the length of arrays of (xv) and (yv). The original sizes of matrices A and Rflip were 3*3 for each element. So I managed to replicate the matrix to [3 3 3 3] for (xy) and (xv) While for B was 3*1 and I made it into [3 1 3 3]. When I tried to execute the line with the replicated matrices for A, Rflip and B {while excluding the counter} I got this error message:
??? Error using ==> mtimes Input arguments must be 2-D.
So would you kindly help me in giving the length of arrays for (xv) and (xy) the same value of M without using the lengthy for loop method while including the counter?
Best wishes
AA
  댓글 수: 3
Azza
Azza 2012년 10월 19일
Sorry. I think I have made a typing error.
counter = counter+1;
So the final line should look something like this:
M(:,2,X,Y) = A*Rlip*M(:,1,X,Y)+B;
Best wishes
Azza
Matt J
Matt J 2012년 10월 19일
편집: Matt J 2012년 10월 19일
What prevents the counter from reaching a value greater than size(M,2) and causing an index-out-of-range error?
In other words, are you sure that length(xv)*length(yv)<=size(M,2)?
Or, are you deliberately resetting the counter to 1 in every pass through the loop? If so, why use it?

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

답변 (2개)

Sachin Ganjare
Sachin Ganjare 2012년 10월 18일

Matt J
Matt J 2012년 10월 18일
This might be what you want
M=mtimesx(A*Rflip, M(:, 1, 1:length(xv), 1:length(yv)) );
M=bsxfun(@plus,M,B);
It requires mtimesx available here

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by