How to add elements to the matrix with a certain pattern?

I have searched the code some time ago, but couldn't find it.
My case is like this.
I have a matrix. eg matrix A. I want to sum the elements of the matrix based on a pattern. The pattern I want is A (a, b) + A (b, g) + A (g, j) + .... So initially we select certain elements from A, for example A (a, b), with row a and column b.
Then the element we select will be added to the next element in the same row number as the column number of the previous element. for example in A (a, b) the column is b, then the next element b will be selected as the row, so that A (b, something from a-b is selected according to the matrix element).
so later there will be some addition 1. A (a, b) + A (b, c) 2. A (a, b) + A (b, d) 3. A (a, b) + A (b, e) etc
This is the example matrix
clc;
clear;
A = [1 2 3 4;
4 3 2 1;
5 2 3 1];
size(A)
thankyou in advance

10 commentaires

For this example "A"
A = [1 2 3 4;
4 3 2 1;
5 2 3 1];
What would be desired result?
For example, I choose A(2,2) that means I choose 3. Then A(2,2) will add to
  1. A(2,2) + A(2,1)
  2. A(2,2) + A(2,3)
  3. A(2,2) + A(2,4)
the first result, the impact of choosing A (2,2) will have 3 different additions like the points above. Then each of these points will have some adds similar to A (2,2). For example for the first point A (2,2) + A (2,1) will have the adds as follows:
  1. A(2,2) + A(2,1) + A(1,1)
  2. A(2,2) + A(2,1) + A(1,2)
  3. A(2,2) + A(2,1) + A(1,3)
  4. A(2,2) + A(2,1) + A(1,4)
and the pattern will continue
A(2,2) + A(2,1)
A(2,2) + A(2,3)
A(2,2) + A(2,4)
These are additions of some values. I do not understand the relation to "I choose A(2,2) that means I choose 3". Where are the results of these 3 additions stored? Do you modify the elements of A?
I did not modify A. A (2,2) means the element of the matrix A which is located in the second row and second column.
So A (row, column). You can see that the element of A which is in row-2 and column-2 is 3.
There is no relationship between the value of 3 in A (2,2) and other additions that will be formed.
it's just like this
A (2,2) + A (2,1) = 3 + 4
A (2,2) + A (2,3) = 3 + 2
that happened after
A (2,2) + A (2,1)
A (2,2) + A (2,3)
A (2,2) + A (2,4)
are each addition will form several new additions, for example I use A (2,2) + A (2,1) in the comments above. The same pattern will happen to A (2,2) + A (2,3) and A (2,2) + A (2,4).
but the addition will stop when it reaches the limit of the matrix size or reaches a certain element that I choose.
I still do not get, what you want to achieve. Kalyan asked already, what the wanted result is for the shown matrix. You mention a lot of addition, but what do you want to do with the results?
Please post a small example with inputs and the wanted output.
I will try to give the whole workflow. But first, I will modify the A matrix.
So, I have
A = [ 1 3 2;
2 3 1;
3 1 2]
then I chose A(2,2) as the initial element (I'll say next as a starting point) and chose A (3,3) as the last element of the addition (I'll say as the destination point).
You can see this picture to make it easier to see the flow of the addition later
then the program will display some addtions in command windows
fisrt addition
A(2,2) + A(2,1) + A(1,1) + A(1,2) + A(2,3) + A(3,3) = 3+2+1+3+1+2
or you can see this image to know the addition flow
second addition
A(2,2) + A(2,1) + A(1,1) + A(1,3) + A(3,3)= 3+2+1+2+2
or you can see this image to know the addition flow
third addition
A(2,2) + A(2,1) + A(1,2) + A(2,3) + A(3,3) = 3+2+3+1+2
or you can see this image to know the addition flow
fourth addition
A(2,2) + A(2,1) + A(1,3) + A(3,3) = 3+2+2+2
or you can see this image to know the addition flow
fifth addition
A(2,2) + A(2,3) + A(3,3) = 3 + 1 +2
or you can see this image to know the addition flow
sixth addition
A(2,2) + A(2,3) + A(3,1)+A(1,1)+A(1,2)+A(2,1)+A(1,3)+A(3,3) = 3+1+3+1+3+2+2+2
or you can see this image to know the addition flow
seventh addition
A(2,2) + A(2,3) + A(3,2)+A(2,1)+A(1,1)+A(1,3)+A(3,3)= 3+1+1+2+1+2+2
or you can see this image to know the addition flow
Note : elements that have been used in the addition pattern, will no longer be used for the same addition or each element can only be added once in one addition
for example,
A(2,2)+A(2,3)+A(3,2)+A(2,1)+A(1,1)+A(1,2)+A(2,1)+A(1,3)+....
or you can see this image. Notice that A (2,1) is used 2 times
In other words: You select two different elements of the matrix (randomly?). Then you want to obtain all permutations (no repetitions, but does the order matter?) (or just some random ones?) of the other elements with 0 to numel(A)-2 elements. Finally you want to add the elements and maybe collect the output in a vector?
Is this correct? Then please define the missing details in the parentheses.
You mention the additions, but what is the wanted result?
yes, I select two different elements. One is as a initial point/starting point and the other is as ending point. It is not randomly. I chose it.
For the order, if you mean only the addition order(not the pattern like addition flow in the image), it doesn't matter which is the first addition, which is the second addition and so on. the most important thing is to fit the pattern. But if it concerns the flow of the addition (of course this is a pattern), it is of course important to follow the pattern.
My expected results are of course in vector form. For example, in the first addition above the results I hope are
A(2,2) + A(2,1) + A(1,1) + A(1,2) + A(2,3) + A(3,3) = 12
as well as the next addition. I want all the results of each addition to be shown.
Regarding the details, I think the only requirement is that there are no repeated elements in the pattern

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 22 Fév 2021
Modifié(e) : Jan le 24 Fév 2021
A = [1 2 3 4;
4 3 2 1;
5 2 3 1];
Ini = [2, 2];
Fin = [3, 4];
SumAllPaths(A, Ini, Fin);
function S = SumAllPaths(A, Ini, Fin)
% Get linear indices:
iIni = sub2ind(size(A), Ini(1), Ini(2))
iFin = sub2ind(size(A), Fin(1), Fin(2))
Ind = 1:numel(A);
Ind([iIni, iFin]) = []
nInd = numel(Ind);
S = [];
for Len = 1:nInd
% Ordered permutations of elements of A without A(Ini) and A(Fin):
I = nchoosek(1:nInd, Len);
% Does the order matter or not? Maybe (un)comment the next line:
I = reshape(I(:, perms(Len:-1:1)), [], Len);
% [EDITED] Typo, "Result" -> "S"
S = cat(1, S, A(iIni) + sum(A(Ind(I)), 2), A(iFin));
end
end

6 commentaires

Ari Ria
Ari Ria le 23 Fév 2021
Modifié(e) : Ari Ria le 23 Fév 2021
I got an error
iIni =
5
iFin =
12
Ind =
1 2 3 4 6 7 8 9 10 11
Undefined function or variable 'Result'.
Error in SumAllPaths (line 16)
S = cat(1, Result, ...
Error in rizki (line 15)
SumAllPaths(A, Ini, Fin);
Then I changed (I dont know this is right or no)
Result
be
'Result'
and I got this errror
'
iIni =
5
iFin =
12
Ind =
1 2 3 4 6 7 8 9 10 11
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in SumAllPaths (line 16)
S = cat(1, 'Result', ...
Error in rizki (line 15)
SumAllPaths(A, Ini, Fin);
Note : I separate the function file and the script because I'm using R2016a
I've fixed this typo in the code. Instead or Result, the output is collected in the variable S .
Why did you insert the string 'Result'. Guessing is not useful during programming. Do you understand, what my code does?
Ari Ria
Ari Ria le 24 Fév 2021
Modifié(e) : Ari Ria le 24 Fév 2021
yes, I insert the string because I think Result is not a variable on your code.
But, why the results of
SumAllPaths(A, Ini, Fin);
are like this
....
30
30
30
30
30
30
30
30
1
>>
There were a lot of numbers 30 before, I just wrote them as dots (....).
I mean, Its not like the pattern because there are too many results.
Ari Ria
Ari Ria le 24 Fév 2021
Modifié(e) : Ari Ria le 24 Fév 2021
I thought something was missing.
As an example I use a 3x3 matrix like the example I gave after I run the program there are about 13700 numbers (based on my understanding of your code about the result of that function). I find out by using
size(SumAllPaths(A, Ini, Fin))
I think the number of additions is not much different from what I described as in the example (in the example I gave 7 different additions). may need to add a conditional code (if). But I don't know how to add the conditions to this condition code based on my explanation in the previous example.
In addition, I thought the looping conditions in your code might be correct according to my explanation of the pattern. it's just that a conditional code may be required.
My code replies a lot of equal values, if you enable the coutung of the permutations:
sum(A([1,2,3])) == sum(A([1,3,2])) == sum(A([3,2,1])) ... and so on
You did not give an example yet and so I have to guess, if this is wanted or not. If you do not want to count the permutations, comment this line:
I = reshape(I(:, perms(Len:-1:1)), [], Len);
I don't understand if you say I haven't set an example. I think in the comments above I've given an example and how it works. then if the sequence you are referring to is from A [2,2] to A [2,1] then the path returns from A [2,1] to A [2,2], then it does not apply. Because from the example above I have illustrated that the path is only one direction and does not return to the previous point.
or maybe you understand the greedy first research algorithm? the algorithm is similar to mine, but not the same.

Connectez-vous pour commenter.

Catégories

Question posée :

le 20 Fév 2021

Commenté :

le 25 Fév 2021

Community Treasure Hunt

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

Start Hunting!

Translated by