having problem to solve somthing in matlab

i have this question it says that i have a matrix A [m,n]
and i need to get the amount of the first row and the last row for example if i have a matrix like this :
7 5 3
9 1 8
10 1 3
i wanna get the first row ==> 7+5+3 =15
and the last row ==>10+1+3=14
and after that i need to do 15+14=29 ( as first row + the last row)
so the problem i have that i cant use sum function but i can use for
can anyone help me with this ?

 Réponse acceptée

Jon
Jon le 2 Juil 2019
If you think about how basic matrix, vector multiplication works, you can see that you can sum rows of a matrix by multiplying by a column vector of ones. You can sum selected elements of a column vector by premultplying by a row vector consisting of zeros and ones (with zeros for the elements that you don't want to include in the sum). Putting these ideas together in your case gives
y = [1 0 1]*A*[1;1;1]
You could also accomplish what you want using MATLAB's indexing abilities along with the sum function. Note that A(1,:) means row 1, every column of A. Similarly A(3,:) means row 3 every column of A
y = sum(A(1,:)) + sum(A(3,:))

5 commentaires

ays ays
ays ays le 2 Juil 2019
thank you for that but i gave an exmaple the matrix i work on i dont what number in each row
so i need help with that and i cant sum at all
i think there is no other way than loop if u have idea
thank you
Jon
Jon le 2 Juil 2019
I'm sorry, but I do not understand what you are trying to say in your comment. Please clarify what is the problem that you are trying to solve that you think you can only do using a loop.
ays ays
ays ays le 2 Juil 2019
Modifié(e) : ays ays le 2 Juil 2019
i am sorry i am kinda weak in english trying my best and i am solving exams to get better at matlab so the question is saying i have a matrix i dont know how many rows in it or columns and in the question it says also i cant use sum
so thats why i need other way if i could use sum i will have done long time ago
and again i am sorry too weak in english >_<
So if the problem is that you want the sum of the elements of the first and last rows of an arbitrary matrix A without using the sum function, you could generalize the approach I gave earlier using:
[m,n] = size(A)
y = [1 1]*[A(1,:);A(m,:)]*ones(n,1)
ays ays
ays ays le 2 Juil 2019
thx

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 2 Juil 2019
Modifié(e) : Jan le 2 Juil 2019
S = sum(x)
is equivalent to
S = 0;
for k = 1:numel(x)
S = S + x(k);
end
If you want to, you can call this as a subfunction. Or you can collect the 2 sums in one loop:
Sfirst = 0;
Slast = 0;
[s1, s2] = size(matrix);
Then run a loop over the columns of the matrix and accumulate the 2 sums.

1 commentaire

Jon
Jon le 2 Juil 2019
I was thinking the OP was specifically looking for a solution that did not utilize either the sum function or looping, which is why I suggested the matrix multiply approach.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Question posée :

le 2 Juil 2019

Commenté :

Jon
le 2 Juil 2019

Community Treasure Hunt

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

Start Hunting!

Translated by