MATLAB Answers

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

59 views (last 30 days)
Ari Ria on 20 Feb 2021
Commented: Ari Ria on 25 Feb 2021
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 CommentsShowHide 9 older comments
Ari Ria on 22 Feb 2021
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

Sign in to comment.

### Answers (1)

Jan on 22 Feb 2021
Edited: Jan on 24 Feb 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 CommentsShowHide 5 older comments
Ari Ria on 25 Feb 2021
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.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!