hi every one , how can i do a for cycle with diffrent vector length , thank you

1 回表示 (過去 30 日間)
mina massoud
mina massoud 2019 年 6 月 5 日
コメント済み: mina massoud 2019 年 6 月 6 日
clear all
clc
X=rand(17,1)
Y=rand(31,1)
Z=rand(28,1)
K=rand(31,1)
n=31
for i=1:n
A=[X(:,n) , Y(:,n) ,Z(:,n) ,K(:,n)];
AT{1,n}= inv(A{1,n}'*A{1,n})*A{1,n}';
end
  2 件のコメント
Rik
Rik 2019 年 6 月 5 日
What do you mean? You aren't using standard Matlab terms, nor are you describing any errors, nor what your goal is. Have a read here and here. It will greatly improve your chances of getting an answer.
I do notice that your vectors have different lengths, yet you want to put them together? What should A look like when n is 18? Should it be a 3-element vector?
And why are you selecting all rows and non-existant later columns for n>1? And why are you using curly braces on a non-cell array?
mina massoud
mina massoud 2019 年 6 月 6 日
sorry if first i didnt explain well my problem ,
i mean that i need to calcolate AT in this condition with a cycle for :
1) for the first 17 cycle i need to calcolate AT for all X Y Z K
2) from 18 to 28 in need to calcolta AT for Y Z K
3) from 29 till the end i need to calcolate only for Y K
thank you for your time

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

採用された回答

Rik
Rik 2019 年 6 月 6 日
You can put them in a cell array and let cell2mat handle the removal of empty elements. I have no clue what you want to do with that variable inside your loop, so I replaced it with sum to have a proof of principle. (also, no need for clear all)
X=rand(17,1);
Y=rand(31,1);
Z=rand(28,1);
K=rand(31,1);
A=cell(numel(X),4);
A(1:numel(X),1)=num2cell(X);
A(1:numel(Y),2)=num2cell(Y);
A(1:numel(Z),3)=num2cell(Z);
A(1:numel(K),4)=num2cell(K);
AT=zeros(1,size(A,1));
for n=1:size(A,1)
B=A(n,:);B=cell2mat(B);
AT(n)= sum(B);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by