1 view (last 30 days)

Show older comments

Hi Matlab Experts, Here is a numerical example of what I wish to vectorize:

A =magic(25);

for y = 1:10

for x = -10:10

if x > 0

A1= A(1+x:end, 5+y:end);

A2= A(1+x:end, 2+y:end);

else

A1= A(1:x+end, 5+y:end);

A2= A(1:x+end, 2+y:end);

end

A3 = A1-A2;

S = sum(A3(:).^3);

end

end

I wish to get rid of both for loops and wish to get the results S & A3 as a set of values in an array in fewer commands and faster mode as I need to use multiple times, I need to save every second that I can. Kindly help.

Cedric Wannaz
on 26 Aug 2013

Edited: Cedric Wannaz
on 26 Aug 2013

You should provide a numeric example, e.g. with a 4x4 array A, and show us a few steps of what you want to achieve. In your question/comments, the following issues prevent us to understand your goal:

- Negative indices as mentioned by Kye.
- Even if indices were positive integers, the way you redefine A makes it shrink at each iteration of the loop.
- Even if this is what you want to achieve, the static range coded in the FOR loop is likely not to be adequate as size(A,1) would change at each iteration.
- In your comment to Kye, you have i1=1:size(i,1) which is 1, so this example is probably not what you want to do.

Cedric Wannaz
on 28 Aug 2013

Your code cannot work, as A1 and A2 won't have the same size and hence cannot be subtracted. It would be easier to help you if we knew what you want to do (which we cannot determine based on your code). So please build a small example with e.g. a 5x5 matrix and explain what it is that you want to compute. For example:

Assume we have the matrix

>> A = magic(5)

A =

17 24 1 8 15

23 5 7 14 16

4 6 13 20 22

10 12 19 21 3

11 18 25 2 9

I want to create a matrix with the lower triangular part (not including the diag.) computed as the lower triang. part of A minus the upper triang. part of A transposed. I want therefore to compute

A3 = A1 - A2 ;

with A1 defined as

0 0 0 0 0

23 0 0 0 0

4 6 0 0 0

10 12 19 0 0

11 18 25 2 0

and A2 defined as

0 0 0 0 0

24 0 0 0 0

1 7 0 0 0

8 14 20 0 0

15 16 22 3 0

So, element (2,1) of A3 for example is computed as 23-24=-1.

With this type of description, we would understand what you want to achieve and be able to propose a solution.

Kye Taylor
on 26 Aug 2013

Edited: Kye Taylor
on 26 Aug 2013

I do not see how the example you provided will execute if A is any standard MATLAB data type.

In particular, if A is a MATLAB variable, such as a cell or a matrix, it can only be indexed using positive integers. In your example, when i is -5 (the first time through your for-loop), the expression A(i+2:end,:) is equivalent to A(-3:end,:), which is invalid since -3 is not a positive integer.

David Sanchez
on 26 Aug 2013

If X is your matrix, here is how you get a 4x4 cell array out of your original matrix:

X = rand(20);

Y = mat2cell(X, repmat(5,[1 size(X,1)/5]), repmat(5,[1 size(X,2)/5]));

Adapt it to your needs.

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

Start Hunting!