matrix generation without a for loop
1 view (last 30 days)
Show older comments
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 Comments
Matt J
on 19 Oct 2012
Edited: Matt J
on 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?
Answers (2)
Matt J
on 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
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!