How to store multiple column vector generated from a for loop?

8 views (last 30 days)
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

Stephen23
Stephen23 on 12 Nov 2018
Edited: Stephen23 on 12 Nov 2018
N = 41;
A = nan(12,N);
B = nan(12,N);
C = nan(12,N);
D = nan(12,N);
for k = 1:N
[A(:,k),B(:,k),C(:,k),D(:,k)] = fun(k-1);
end
This will give four matrices with all of the output data.
  6 Comments
Mr. 206
Mr. 206 on 12 Nov 2018
Edited: Mr. 206 on 12 Nov 2018
Great! A slight mistake in my `func` file.
Thanks
Can you help a bit more?
After completing the procedure for A there is 12 by N matrix. How can i find out the smallest value in each row of that matrix and if the smallest value is found for which delta is responsible for the the smallest value?
So that i can find a matrix A which is a 12 by 1 matrix comprised of all the smallest value in each row. And another 12 by 1 matrix which is comprised of the delta value responsible for those smallest values.
I hope my Question is clear. Again thanks for the previous help.
Stephen23
Stephen23 on 12 Nov 2018
Edited: Stephen23 on 12 Nov 2018
"How can i find out the smallest value in each row of that matrix and if the smallest value is found for which delta is responsible for the the smallest value?"
[val,idx] = min(A,[],2)
delta(idx)

Sign in to comment.

More Answers (1)

KSSV
KSSV on 12 Nov 2018
Edited: KSSV on 12 Nov 2018
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

Sign in to comment.

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!