How to add elements to the matrix with a certain pattern?
Afficher commentaires plus anciens
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
KALYAN ACHARJYA
le 20 Fév 2021
For this example "A"
A = [1 2 3 4;
4 3 2 1;
5 2 3 1];
What would be desired result?
Ari Ria
le 21 Fév 2021
Jan
le 21 Fév 2021
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?
Ari Ria
le 21 Fév 2021
Ari Ria
le 21 Fév 2021
Ari Ria
le 21 Fév 2021
Jan
le 21 Fév 2021
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.
Ari Ria
le 22 Fév 2021
Jan
le 22 Fév 2021
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?
Ari Ria
le 22 Fév 2021
Réponses (1)
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
Jan
le 24 Fév 2021
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?
Jan
le 24 Fév 2021
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);
Ari Ria
le 25 Fév 2021
Catégories
En savoir plus sur Multidimensional Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








