Hello,i have a vector named dem(101) and a matrix routes(21,14) that has inside the numbers 1,2,3,4 etc. for example the first row of the matrix is the routes(1,14)=[1 21 25 26 28 30 31 29 24 23 22 48 1]. I want to save to a matrix named load(21) the sum of every route by the vector dem.For example i want the load(1)=sum(dem[1] dem[21] dem[25] dem[26]...... etc)

 Réponse acceptée

Star Strider
Star Strider le 21 Mar 2015

0 votes

I don’t understand your matrix structure, but this will do the calculation you described:
dem = randi(25, 1, 101);
routes = [1 21 25 26 28 30 31 29 24 23 22 48 1];
load = sum(dem(routes));
This illustrates the details of the addressing:
dem_routes = dem(routes); % Check
I include it for illustration only. It is not part of the code.

4 commentaires

Giannakis Stoukas
Giannakis Stoukas le 21 Mar 2015
??? Subscript indices must either be real positive integers or logicals.
The message i get all the time.What that means?
Star Strider
Star Strider le 21 Mar 2015
Modifié(e) : Star Strider le 21 Mar 2015
Did you get it with my code? It ran for me without error as posted.
If you are doing the sum for various rows of your ‘routes’ matrix, it could be that some elements are zero or negative, or floating-point numbers rather than integers. Those are not allowed as index references in MATLAB. (The row of ‘routes’ you posted did not have any of those problems.)
You did not attach your ‘routes’ matrix, so I am guessing as to what the problem may be, on the basis of the error you reported.
Giannakis Stoukas
Giannakis Stoukas le 22 Mar 2015
Yes i had one zero element without noticing it.Thank you for your respond
Star Strider
Star Strider le 22 Mar 2015
My pleasure!
It should work without the zero element.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by