Effacer les filtres
Effacer les filtres

matrix generation without a for loop

2 vues (au cours des 30 derniers jours)
Azza
Azza le 18 Oct 2012
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 commentaires
Azza
Azza le 19 Oct 2012
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 le 19 Oct 2012
Modifié(e) : Matt J le 19 Oct 2012
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?

Connectez-vous pour commenter.

Réponses (2)

Sachin Ganjare
Sachin Ganjare le 18 Oct 2012

Matt J
Matt J le 18 Oct 2012
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

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by