What will be last elements in Iterative addition for loop?

조회 수: 1 (최근 30일)
vimal kumar chawda
vimal kumar chawda 2021년 6월 6일
답변: Manas Minnoor 2021년 6월 6일
a = randi(10,1,6);
b = randi(10,6,1);
c = randi(10,6,4);
d = randi(10,4,3);
e = randi(20,20,1)-10;
Task 1a
MATLAB has a builtin sum function. To get acquainted with loops, pretend
you could not use that function, and instead recreate the result using a
for-loop.
Create a variable that will hold the result and initialize it with 0.
Then iteratively add each element of the vector to the result. You can
check your result with the builtin function.
i=[a b c d e];
for j=1:length(a) -1
sum1(:,j)= a(:,j)
sum_iter(:,j) = sum1(:,j)+ sum1(:,j+1)
j=j+1
end
How can i know what is the last element in sum_iter? I am not sure if I have 7 column in a datasets. This loop is only for a data sets. Thanks

답변 (1개)

Manas Minnoor
Manas Minnoor 2021년 6월 6일
Hi Vimal,
You may access the last element of a matrix using the end keyword.
Hope this helps.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by