Help with column-wise matrix manipulation without for loop

5 vues (au cours des 30 derniers jours)
J AI
J AI le 28 Juin 2020
Commenté : J AI le 29 Juin 2020
I have the matrix (which is basically the matrix representation of several lines)
A = [10 5 10 % first two columns are the gradients in x-direction and the last column is the y-intercept
0 4 3
2 0 8
0 6 5];
I want to find the intersection between the lines starting from bottom going up (for each column seperately) when the gradients are non-zero, e.g., the last row of first column has zero gradient so I ignore it and move up to third row, but the second row has zero gradient as well, so I calculate its intersection with the first row:
The first row elements are always non-zero and since there is no "previous row" before the first row, the intersection of the those lines by default are set to zero. I have achieved my target using for loop, but I was hoping to avoid for loops if possible (I will have more columns than just 3). Here is what I have done:
for k = 1:2
B = A(A(1:end,k)~=0,[k,n]); % for each column, I am finding the matrix that does not include zero gradients
[r,~] = size(B);
c = 2:r;
if isempty(c)
x{k} = 0;
else
x{k} = [0; (B(c,end)-B(c-1,end))./(B(c-1,1)-B(c,1))];
end
end
which gives me my desired results:
>> x{1}
ans =
0
-0.2500
>> x{2}
ans =
0
-7
-1
Notice how the dimensions of x{1} and x{2} are different, and thus I had to use curly brackets. Avoiding the for loop can save me great deal of computational time. Thanks in advance! Let me know if you have any question.

Réponses (1)

Shojiro SHIBAYAMA
Shojiro SHIBAYAMA le 29 Juin 2020
Honestly, I didn't get what you want do precisely, but the following code may help you. IMO, for loop in MATLAB doesn't prolong the computation time.
dAn_p = A(2:end,:)-A(1:(end-1),:); % 'next' and 'previous' matrix from A.
tmp = cell2mat(arrayfun(@(ix)-dAn_p(:,end)./dAn_p(:,ix), 1:3,'un',0)); % take the gradient
tmp(A(2:end,:)==0|A(1:(end-1),:)==0)=0 % set elements zeros where any corresponding elements in A are zeros.
  1 commentaire
J AI
J AI le 29 Juin 2020
Basically, the first two columns decide the computations to be done, however, they are seperate entitites and do not affect each other. Now this is how the computation works:
The basic idea: Intersection is only calculated between the current line and the preceding non-zero gradient line. The calculations are done from bottom to up.
The first column: the very last row has zero, so I move up and find non-zero in 3rd, however, 2nd has zero, so I move up again and find non-zero in the 1st. So I calculate the intersection between the two lines with gradients 2 and 10 and y-intercepts 8 and 10 respectively. However, since there are no more lines prior to the first row, I set its intersection to zero. Total of two intersecting points for column 1 and thus x{1} = [0; -0.25].
The second column: Starting from bottom, last row is non-zero but 3rd is zero, so the intersection is calculated between last and 2nd (non-zero gradient). Similarly, intersection between 2nd and 1st row (non-zero gradient) is calculated. By default, first row's intersection is set to zero. Total of three intersecting points for column 2 and thus x{2} = [0; -7; -1].
I hope I could make myself clear about the problem.
By the way, I didn't understand the code you provided. Could you kindly explain?
Thank you for your effort!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by