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

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 23 de Jul. de 2016
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)

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 23 de Jul. de 2016

Respondida:

el 23 de Jul. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by