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

 Accepted Answer

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)

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!