Creating a matrix with rows and collumns

Hey guys. I have a matrix with 500 rows and 60 collumns of only numerical data. I have a script does calculates all possible summations between two rows (for example: row 1 + row 2, row 1 + row 3, row 1 + row 4 etc. When the script runs, Matlab calculates all summations, but instead of adding each calculation to a row, it overwrites the rows and in the end gives me only one row with data (as the rows are suppose to be 500x 500.
Thank anyone that can help me.
Tsvetelina

5 comentarios

EmirBeg
EmirBeg el 21 de Mayo de 2021
Could you show us your code so we can tell you what to change?
Tsvetelina Tomova
Tsvetelina Tomova el 21 de Mayo de 2021
Hey, yes. here it is:
per isakson
per isakson el 21 de Mayo de 2021
Editada: per isakson el 21 de Mayo de 2021
Your code overwrites test in every iteration.
Prepare a MWE:
  • <4x4> rather <500x500>
  • small whole numbers only (easier to debug)
  • a sample subplots matrix
  • expected result; matrix test
Make the MWE easy to download and use
Torsten
Torsten el 21 de Mayo de 2021
Editada: Torsten el 21 de Mayo de 2021
test(i,j,:) = subplot(i,:) + subplot(j,:) ;
Since test(i,j,:) = test(j,i,:), you can save some time if your inner loop only goes from j = i:500.
KSSV
KSSV el 21 de Mayo de 2021
You have to initilaize test first before the loop and then include indices in test while calculating the sum.

Iniciar sesión para comentar.

Respuestas (1)

Kartikay Sapra
Kartikay Sapra el 21 de Mayo de 2021
subplots = [1 2; 3 4; 5 6]
[rows cols] = size(subplots);
test = zeros([rows*(rows+1)/2 cols]);
k = 1;
for i = 1:rows
for j = 1:cols
test(k,:) = subplots(i,:) + subplots(j,:);
k = k+1
end
end
test
Try this code, for your code, rows = 500 and cols = 500. Store your calculations at a new index every time.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 21 de Mayo de 2021

Respondida:

el 21 de Mayo de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by