how to apply for loop for different vectors

I have two vectors or more depending upon the user, and I want to run the program many times depending upon this number of vectors. Then, for each run, I need to check the length of each vector and apply its related formulas depending upon the its length. I wrote the following program but it does not work, it takes only the second vector!!
M=[1 2 3 4];
M=[6 7 7 8 2 1 3 2];
k=length(M)
number_of_vectors=2;
for i=1:number_of_vectors
if k==4
h=M-1
elseif k~=4
h=M-3
end
end

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 23 日

0 投票

Use cell arrays
clear
V={[1 2 3 4],[6 7 7 8 2 1 3 2]}
number_of_vectors=numel(V);
for ii=1:number_of_vectors
M=V{ii}
k=numel(M)
if k==4
h{ii}=M-1
else
h{ii}=M-3
end
end
celldisp(h)

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by