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

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.

 採用された回答

Stephen23
Stephen23 2018 年 11 月 12 日
編集済み: Stephen23 2018 年 11 月 12 日

0 投票

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 件のコメント

Mr. 206
Mr. 206 2018 年 11 月 12 日
I am having problem with delta = 0: 0.077: 0.385. How to use this values?
Stephen23
Stephen23 2018 年 11 月 12 日
delta = 0:0.077:0.385;
N = numel(delta);
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(delta(k));
end
Mr. 206
Mr. 206 2018 年 11 月 12 日
編集済み: Mr. 206 2018 年 11 月 12 日
I did this, but the problem is all the values are same as A.
delta = 0:0.077:0.385;
N = numel(delta);
A = nan(12,N);
B = nan(12,N);
C = nan(12,N);
D = nan(12,N);
for kk = 1:N
A(:,kk) = func(delta(kk));
B(:,kk) = func(delta(kk));
C(:,kk) = func(delta(kk));
D(:,kk) = func(delta(kk));
end
A
B
C
D
Stephen23
Stephen23 2018 年 11 月 12 日
"I did this, but the problem is all the values are same as A."
No, you actually did something else entirely: you called your function four times.
Your code:
for kk = 1:N
A(:,kk) = func(delta(kk));
B(:,kk) = func(delta(kk));
C(:,kk) = func(delta(kk));
D(:,kk) = func(delta(kk));
end
My code:
[A(:,k),B(:,k),C(:,k),D(:,k)] = fun(delta(k));
Do you see that my code has four output arguments from fun, just like your original function? Whereas you, for no obvious reason, decided to call your function four times, each time with just one output argument. So no surprises that your code will just get the same output four times.
Mr. 206
Mr. 206 2018 年 11 月 12 日
編集済み: Mr. 206 2018 年 11 月 12 日
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 2018 年 11 月 12 日
編集済み: Stephen23 2018 年 11 月 12 日
"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)

サインインしてコメントする。

その他の回答 (1 件)

KSSV
KSSV 2018 年 11 月 12 日
編集済み: KSSV 2018 年 11 月 12 日

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 件のコメント

Mr. 206
Mr. 206 2018 年 11 月 12 日
Actually my real value for delta is 0: 0.077: 0.385. How to use this values?
KSSV
KSSV 2018 年 11 月 12 日
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
Mr. 206 2018 年 11 月 12 日
Here you store all A,B,C,D together. I wanted all the A together in one column matrix, then in another matrix all the B, in aother all the C....... together.
KSSV
KSSV 2018 年 11 月 12 日
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
KSSV 2018 年 11 月 12 日
YOu should check your function output...
Mr. 206
Mr. 206 2018 年 11 月 12 日
Thanks! It was my Mistake. Thanks for your help

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2018 年 11 月 12 日

編集済み:

2018 年 11 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by