How to store multiple column vector generated from a for loop?
Show older comments
I have a function called func which returns A, B, C and D. This A, B, C, D each are 12 elements column vector. I am using this code
for delta = 0: 1: 40
[A,B,C,D] = func(delta);
end
Now after completing the for loop i will get 41 sets of A, B, C and D. How can i store them together?
I tried this
store = 0
for delta = 0: 1: 40
[A(store),B(store),C(store),D(store)] = func(delta);
end
But obviously there is an error like A(I) = X: X must have the same size as I. cause A is storing a 12 element column vector.
Accepted Answer
More Answers (1)
clc; clear all ;
iwant = zeros(4,12,40) ;
for delta = 1: 40
[A,B,C,D] = func(delta);
iwant(:,:,delta) = [A B C D] ;
end
Note that MATLAB index starts from 1 not zero.
6 Comments
Mr. 206
on 12 Nov 2018
KSSV
on 12 Nov 2018
delta = 0: 0.077: 0.385 ;
N = length(delta) ;
iwant = zeros(4,12,N) ;
for i = 1:N
[A,B,C,D] = func(delta(i));
iwant(:,:,i) = [A B C D] ;
end
Mr. 206
on 12 Nov 2018
KSSV
on 12 Nov 2018
delta = 0: 0.077: 0.385 ;
N = length(delta) ;
A = zeros(12,N) ;
B = A ;
C = A ;
D = A ;
for i = 1:N
[a,b,c,d] = func(delta(i));
A(:,i) = a ; B(:,i) = b ;
C(:,i) = c ; D(:,i) = d ;
end
KSSV
on 12 Nov 2018
YOu should check your function output...
Mr. 206
on 12 Nov 2018
Categories
Find more on Creating and Concatenating Matrices 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!