dot product and indexing

8 views (last 30 days)
kalana agampodi
kalana agampodi on 21 Oct 2021
Commented: De Silva on 24 Oct 2021
I am reviewing this code and I do not undestand what this code means and how the dot operation work.
Can you please explain with some example ?
NCol = [
1 3 1 2 4 3 5 2 3 1 2 5 ]
NRow = [
1 1 2 2 2 3 3 4 4 5 5 5 ]
c=(NCol(1:k-1) == NRow(k)) .* (NRow(1:k-1) == NCol(k))

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 21 Oct 2021
The explantion is as follows:
% Step 1. Comparing every individual value (element of) of NCol row vector (row matrix)
% with the k-th (the last one) element of NRow (i.e. 5) whether their are equal or not.
% If equal then, the result will be 1 (true); otherwise, 0 (false)
(NCol(1:k-1) == NRow(k))
% Step 2. Similar to Step 1, every individual value (element of) of
% NRow row vector (row matrix) with the kth (the last one) element of NCol (row vector)
% whether their are equal or not.
% If equal then, the result will be 1 (true); otherwise, 0 (false)
(NRow(1:k-1) == NCol(k))
% Step 3. Elementwise computation of the comparisons from Step 1 and Step 2
% simultaneously.
(NCol(1:k-1) == NRow(k)) .*(NRow(1:k-1) == NCol(k))
% Finally, the computation results from the two logical vectors from Step 1 and 2 are there
  1 Comment
kalana agampodi
kalana agampodi on 23 Oct 2021
Sorry still not clear,
This is the whole code below and I am trying to undestand it.
I have addd some code but i do not undestad fully whats going on. Can you please explain or write the same code to in a simpler way ?
Thank you
nz=length(NRow);
Degrees=zeros(1,n);
for k=1:nz
if NRow(k)~=NCol(k) % Don't count the diagonal elements in degree.
c=(NCol(1:k-1) == NRow(k)) .* (NRow(1:k-1) == NCol(k));
% The 'c' is to find if the degree has been counted.
if not(max(c))
%if the degree have been counted, don't get in 'if'.
Degrees(NRow(k))=Degrees(NRow(k))+1;
Degrees(NCol(k))=Degrees(NCol(k))+1;
end
end
end

Sign in to comment.


Walter Roberson
Walter Roberson on 21 Oct 2021
The 1:k-1 can only work properly if k is a scalar.
NRow(1:k-1) == NCol(k)
That part tests whether each value in NRow before index k, is equal to the NCol entry at index k. The result is a logical vector of length k-1 . For example if k were 4, then NCol(4) is 2, and you would be testing NRow(1:3) == 2 which would give you the logical vector [false, false, true]
(NCol(1:k-1) == NRow(k))
That part tests whether each value in NCol before index k, is equal to the NRow entry at index k. The result is a logical vector of length k-1 . For example if k were 4, then NRow(4) is 2, and you would be testing NCol(1:3) == 2 which would give you the logical vector [false, false, false]
So you have two logical vectors of length k-1 and the .* multiplies the corresponding entries. Multiplication of corresponding logical values is the same as "and" of the entries.
The code could have been written as
c=(NCol(1:k-1) == NRow(k)) & (NRow(1:k-1) == NCol(k))
and the only difference would have been as to whether c would end up being a double precision vector (original code) or a logical vector (suggested code)
  3 Comments
De Silva
De Silva on 24 Oct 2021
Thanks anyway. It’s not a solution, I’m trying to solve power flow in a 14 bus system. This is used to get the Tinney 0 matrix reordering.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!