Using a for-loop with several input matrices
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
0 comentarios
Respuestas (1)
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
el 18 de Abr. de 2017
Editada: Chris Matthews
el 18 de Abr. de 2017
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.
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices 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!