dot product and indexing

3 次查看(过去 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