Creating a loop in matlab

조회 수: 5 (최근 30일)
Doyinsola Oduwole
Doyinsola Oduwole 2020년 7월 21일
답변: Cris LaPierre 2020년 7월 21일
Hello,
I have a dataset, lets call it I = [10 34 56 76 54 ], and I have another say x = [1 2 3 4...10]
I'm having difficulties writing a code that goes through x and picks a number where the sum of the sqaures of deviations from y and that number equals zero.
I = importdata(......);
for x = 1:10
y =I-x;
z = sum(y.^2);
logic=any(z==0);
end
This code only picks x = 10 and doesnt calculate for all the values of x.

답변 (1개)

Cris LaPierre
Cris LaPierre 2020년 7월 21일
The values of x, y, z and logic are overwritten each loop with the new values. These repeats each loop until the final loop, giving you just the final values for when x=10.
Perhaps try going through Chapter 13 of MATLAB Onramp, which covers if statements and for loops.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by