dot product and indexing

조회 수: 4 (최근 30일)
kalana agampodi
kalana agampodi 2021년 10월 21일
댓글: De Silva 2021년 10월 24일
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))

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 10월 21일
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
kalana agampodi
kalana agampodi 2021년 10월 23일
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

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2021년 10월 21일
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
Walter Roberson
Walter Roberson 2021년 10월 24일
No, I don't think I can explain that. I would have to work backwards and figure out what kind of problem might exist that could lead to that code being considered a solution. Some kind of graph theory algorithm. It might perhaps be calculating the degree of each vertex in a graph.
De Silva
De Silva 2021년 10월 24일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by