MATLAB Answers

Help with column-wise matrix manipulation without for loop

8 views (last 30 days)
J AI on 28 Jun 2020
Commented: J AI on 29 Jun 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;
x{k} = [0; (B(c,end)-B(c-1,end))./(B(c-1,1)-B(c,1))];
which gives me my desired results:
>> x{1}
ans =
>> x{2}
ans =
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.

Answers (1)

Shojiro SHIBAYAMA on 29 Jun 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 Comment
J AI on 29 Jun 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!

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!