필터 지우기
필터 지우기

Using a for-loop with several input matrices

조회 수: 1 (최근 30일)
Chris Matthews
Chris Matthews 2017년 4월 17일
댓글: Jan 2017년 4월 18일
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

답변 (1개)

Jan
Jan 2017년 4월 17일
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
Chris Matthews
Chris Matthews 2017년 4월 18일
편집: Chris Matthews 2017년 4월 18일
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 2017년 4월 18일
Use the debugger to see, what's going on. If this does not reveal the problem, post your code here.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Extend Testing Frameworks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by