how to extract subvectors of a vector or matrix?
12 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Cris LaPierre
on 19 Dec 2020
Edited: Cris LaPierre
on 19 Dec 2020
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.
0 Comments
KSSV
on 19 Dec 2020
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
0 Comments
See Also
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!