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 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