# How using a loop to add up the same numbers in a matrix and store the numbers in a new matrix

2 visualizzazioni (ultimi 30 giorni)
Loo Lee Chong il 20 Mag 2022
Risposto: Chandra il 23 Mag 2022
m=[1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]
for x=1:length(m)
How to compare the data in the matrix and get the same values and add them together then store it in a new matrix.
##### 2 CommentiMostra NessunoNascondi Nessuno
the cyclist il 20 Mag 2022
Do you mean you want to find repeated numbers, and add them?
For
m = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9]
would the output be
output = [2,4,6,8,10,12,14,16,18]
?
Can you give another example or two? Try to make the example a representative one.
KSSV il 20 Mag 2022

Accedi per commentare.

### Risposte (1)

Chandra il 23 Mag 2022
Hi,
Here the code is shown such that the values stored are non repeatednon-repeated and addition of repeated values together
m=[1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9];
%m = [1 2 4 56 3 35 6 1 2 3 4 2];
m1 = m;
b =0;
for x=1:length(m1)-1
for j = x+1:length(m1)
if j>length(m1)
break
end
if m1(x)==m1(j)
b = b+m(x);
m1(j) = '';
if j==x+1
j= j-1;
end
end
end
m2(x) = b;
if x<=length(m1)-1
b = m1(x+1);
end
end
m2 = m2(1:length(m1));
m2 %final output values are stored in m2
Refer to the following documentation for unique values that are not repeated:
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by