How to store multiple column vector generated from a for loop?
Mostra commenti meno recenti
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.
Risposta accettata
Più risposte (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 Commenti
Mr. 206
il 12 Nov 2018
KSSV
il 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
il 12 Nov 2018
KSSV
il 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
il 12 Nov 2018
YOu should check your function output...
Mr. 206
il 12 Nov 2018
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!