Info

この質問は閉じられています。 編集または回答するには再度開いてください。

how do i vectorise given code?

1 回表示 (過去 30 日間)
Vandita
Vandita 2013 年 8 月 26 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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.
  3 件のコメント
Vandita
Vandita 2013 年 8 月 26 日
As I mention in my question, A is a 2 dimensional matrix. havign row and columns and a value for each row and column. I want to modify A using some row column addresses, defined by i and j. And I wnat it all to be done in one go in vectorised mode. for each value of set of i and j I want a new A. somethign like this: A(i,j) or A{i,j} = A(1+i;end, 5+j:end) where i and j could take value from 0 to 5 and say -5 to 5 respectively. i don't want to use for loop. I need vectorisation of above to save time.
Cedric
Cedric 2013 年 8 月 28 日
See my comment below my answer.

回答 (3 件)

Cedric
Cedric 2013 年 8 月 26 日
編集済み: Cedric 2013 年 8 月 26 日
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.
  1 件のコメント
Cedric
Cedric 2013 年 8 月 28 日
編集済み: Cedric 2013 年 8 月 28 日
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
Kye Taylor 2013 年 8 月 26 日
編集済み: Kye Taylor 2013 年 8 月 26 日
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.
  1 件のコメント
Vandita
Vandita 2013 年 8 月 26 日
編集済み: Stephen23 2016 年 1 月 15 日
in that case indexing for output A can be redefined asfollows:
i = (-5: 1:5)' (say)
i1 = 1:size(i,1);
A(i1) can be calculated now to get positive real integer indexing. however the basic issue is to get all As for all is in one go without using for loop for making it fast code. The question is how i do this..

David Sanchez
David Sanchez 2013 年 8 月 26 日
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.

この質問は閉じられています。

Community Treasure Hunt

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

Start Hunting!

Translated by