how to extract subvectors of a vector or matrix?

12 次查看(过去 30 天)
My case is like this
n=100;
x=[1 2 3 4 5 6 7 8 9 10];
y=[1 4 9 16 25 36 49 64 81 100];
A=[x' y'];
I want to extract the data from the vectors x and y, but have it accumulated and stored in subvectors. The result will be subvectors
xx= [1 2] [1 2 3] [1 2 3 4] [1 2 3 4 5] [1 2 3 4 5 6] [1 2 3 4 5 6 7] [1 2 3 4 5 6 7 8] [1 2 3 4 5 6 7 8 9] [1 2 3 4 5 6 7 8 9 10]...;
yy= [1 4] [1 4 9] [1 4 9 16] [1 4 9 16 25] [1 4 9 16 25 36] [1 4 9 16 25 36 49] [1 4 9 16 25 36 49 64] [1 4 9 16 25 36 49 64 81] [1 4 9 16 25 36 49 64 81 100]...;
I need to generate the subvectors xx and yy to plot the data for each of the subvectors. I need to graph the evolution when 1 data is increased.
Example
xx(1)=[1 2];
yy(1)=(1,4);
plot(xx(1),(yy(1))
Finally plot all pairs xx vs yy for loop

回答(2 个)

Cris LaPierre
Cris LaPierre 2020-12-19
编辑:Cris LaPierre 2020-12-19
This sounds like an assignment designed to teach you how to use a for loop. If so, it doesn't sound like you are thinking about how to take advantage of the loop - specifically that the loop counter increases by one each time.
Think about what code would be repetitive here, and put that inside your loop. This page on array indexing may help you. One hint - your for loop counter does not have to start at 1.

KSSV
KSSV 2020-12-19
n=100;
x=[1 2 3 4 5 6 7 8 9 10];
y=[1 4 9 16 25 36 49 64 81 100];
m = length(x) ;
xx = cell(m-1,1) ;
yy = cell(m-1,1) ;
for i = 1:m-1
xx{i} = x(1:i+1) ;
yy{i} = y(1:i+1) ;
end

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by