adding varied length vectors to a new matrix sequentially

조회 수: 1 (최근 30일)
mwa710
mwa710 2013년 3월 20일
I have a for loop which outputs a vector of varying length each time. I want to store each vector as a new row in a new matrix/array/structure (anything). I tried adding a line like this: temp1(:, i) = vec; with my vec to be stored going into a temp matrix. However, this does not work. Any tips on how I can achieve this?

채택된 답변

Matt Tearle
Matt Tearle 2013년 3월 20일
Sounds like a job for a cell array:
x = cell(n,1);
for k = 1:n
vec = ...;
x{k} = vec;
end

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 20일
for k=1:10
vec=randi(10,1,randi(10),1) % your vector
out{k}=vec
end
  댓글 수: 2
mwa710
mwa710 2013년 3월 21일
this is great, thanks! do you now know a way I can extract that values to plot? essentially I want to make a raster plot of these vectors now
Matt Tearle
Matt Tearle 2013년 3월 21일
In general, you can access data in a cell array the same way: y = x{k} (then plot(y) or whatever). What exactly do you mean by a "raster plot"? This term seems to be context/application dependent. Can you describe the graph you're trying to make? I'm going to guess that you'll need to overlay plots using hold, if you're plotting a bunch of different-sized vectors. If so, you could do this inside the same for-loop that you're using to create/calculate the data in the first place.

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by