matrix generation without a for loop

1 回表示 (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by