Array assembly by indexing in a loop

2 Ansichten (letzte 30 Tage)
Saim Ehtesham
Saim Ehtesham am 26 Nov. 2022
Kommentiert: Saim Ehtesham am 26 Nov. 2022
I have the following code:
n = 4;
x1 = linspace(0,2,n);
y1_x1 = sqrt(1-( (x1.^2)/4 ) );
Y1_X1 = []
for i = 1:n
Y1_X1(end) = y1_x1(i:end);
end
The idea is to form Y1_X1 such that first time, it takes n values of y1_x1 (in this case all 4), next time it takes (n-1):end values of y1_x1 (in this case last 3) and assembles these 3 infront of the first 4, and does this all the way till it gets the last value in y1_x1 and assembles it at the end of the long Y1_X1. So output of loop looks like this:
Y1_X1 =
1.0000 0.9428 0.7454 0 0.9428 0.7454 0 0.7454 0 0
Thanks

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 26 Nov. 2022
Bearbeitet: Bruno Luong am 26 Nov. 2022
n = 4;
x1 = linspace(0,2,n);
y1_x1 = sqrt(1-( (x1.^2)/4 ) );
Y1_X1 = [];
for i = 1:n
Y1_X1 = [Y1_X1, y1_x1(i:end)]; % concatenate
end
Y1_X1
Y1_X1 = 1×10
1.0000 0.9428 0.7454 0 0.9428 0.7454 0 0.7454 0 0

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by