How to input a matrix into a variable

Id like to use a single variable to get multiple answers, for example.
%% --------
X = [10:10:100]
Y = X+3
%% --------
how can i get an output of Y for every value of X, i just need it to display, even if it overwrites Y's value each time.

댓글 수: 3

What about
X = [10:10:100];
Y = X+3;
disp( Y )
13 23 33 43 53 63 73 83 93 103
Diego
Diego 2022년 8월 27일
Thanks, that worked and made me realise my issue is in my formula not variable insert.

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

답변 (1개)

Divyam
Divyam 2025년 4월 30일

0 개 추천

Hi @Diego,
You can either iterate through the array X using a for loop and display values of Y for each value of X or directly use the 'disp' function on the array Y and display all the values of Y that are calculated using the linear equation .
% Method 1: Iterate through X and display values of Y for each value of X
X = 10:10:100; % Creates a vector [10 20 30 ... 100]
for i = 1:length(X)
Y = X(i) + 3;
disp(['X = ' num2str(X(i)) ', Y = ' num2str(Y)])
end
X = 10, Y = 13 X = 20, Y = 23 X = 30, Y = 33 X = 40, Y = 43 X = 50, Y = 53 X = 60, Y = 63 X = 70, Y = 73 X = 80, Y = 83 X = 90, Y = 93 X = 100, Y = 103
% Method 2: Display all the values of Y that are solutions of the linear
% equation Y = X + 3
X = 10:10:100;
Y = X + 3;
disp(Y)
13 23 33 43 53 63 73 83 93 103
For more information regarding the 'disp' function, refer to the following documentation: https://www.mathworks.com/help/matlab/ref/disp.html

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2022년 8월 27일

답변:

2025년 4월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by