Borrar filtros
Borrar filtros

Using a for-loop with several input matrices

1 visualización (últimos 30 días)
Chris Matthews
Chris Matthews el 17 de Abr. de 2017
Comentada: Jan el 18 de Abr. de 2017
I have several matrices that I want to run a for-loop through, and am not sure how to do this.
clc
clear all
TEST1 = [0 0 0 0];
TEST2 = [1, 1, 1, 1];
TEST3 = [235.0623 188.2428 44.9479 103.4551];
l = length(TEST3);
for k = 1:l
y(k) = 0;
if (k==1)
w(k) = 1/sqrt(l);
else
w(k) = sqrt(2/l);
end
for n = 1:l
y(k) = y(k)+TEST3(n)*cos(pi*(2*n-1)*(k-1)/(2*l));
end
y(k) = y(k)*w(k);
end
DCTTEST3 = (y)
My for-loop works well, and is currently programmed to use test3 matrix as the input, and show the corresponding matrix output.
Is it possible, without triplicating the code, to use this format but run the three test cases through it, and then have three output matrices? If it is, can you please show me how?
Thanks in advance! Chris

Respuestas (1)

Jan
Jan el 17 de Abr. de 2017
TestSet = {[0 0 0 0], ...
[1, 1, 1, 1], ...
[235.0623 188.2428 44.9479 103.4551]};
DCTTEST = cell(1, numel(TestSet)); % Pre-allocate
for iTest = 1:numel(TestSet)
Test = TestSet{iTest};
l = length(Test);
y = zeros(1, l); % Pre-allocate
w = zeros(1, l);
for k = 1:l
if (k==1)
w(k) = 1/sqrt(l);
else
w(k) = sqrt(2/l);
end
for n = 1:l
y(k) = y(k) + Test(n) * cos(pi * (2 * n-1) * (k-1) / (2*l));
end
y(k) = y(k)*w(k);
end
DCTTEST{iTest} = y;
end
  2 comentarios
Chris Matthews
Chris Matthews el 18 de Abr. de 2017
Editada: Chris Matthews el 18 de Abr. de 2017
When i look at DCTTEST, it only shows this:
DCTTEST =
[1x4 double] [1x4 double] [1x4 double]
And when I look at each of these vectors, they all only show the answer from the third test case?
Jan
Jan el 18 de Abr. de 2017
Use the debugger to see, what's going on. If this does not reveal the problem, post your code here.

Iniciar sesión para comentar.

Categorías

Más información sobre Extend Testing Frameworks en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by