build an array y depending on x

조회 수: 4 (최근 30일)
Mina
Mina 2021년 6월 4일
답변: the cyclist 2021년 6월 4일
Build an array y by doubling all the elements of a vector x that are positive and adds 10 to all the elements of x that are negative. Note that x could be any length.
Initialise x as indicated above and y to an array of zeroes for the same number as elements as x.
Use a loop to change each element of y as described above. Use the variable i as your loop counter.
  댓글 수: 2
Adam Danz
Adam Danz 2021년 6월 4일
What have you tried already to meet these goals?
If you're having trouble getting started, consider going through the Matlab on ramp.
Mina
Mina 2021년 6월 4일
x = input();
for i = 1:x
if x(i) >= 0
y = x(i)* 2;
else
y = x(i) + 10;
end
disp(y);
end

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2021년 6월 4일
You have a few problems to fix:
As you can see in its documentation, the input function requires you to include some prompting text, such as
input('Enter x: ')
Instead of
for i = 1:x
you want to loop from 1 to the number of elements in x (not from 1 to x itself). You should be able to figure that out. It is not difficult.
Finally, you keep overwriting the same scalar value of y, over and over. Instead, you also want to define a vector y, and only write to each element in turn. Again, I bet you can figure that out (since you are doing something similar with x).

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by