Thread Subject: Matrix indexing

Subject: Matrix indexing

From: forty-two

Date: 9 Feb, 2010 04:27:53

Message: 1 of 1

A bit of context first -- I'm trying to make my own DCT function for a
school assignment. I've already gotten it working using a messy, C-
like, slow but correct loop implementation which'll get me all the
points I need. However, for the fun and challenge of it, I'm trying to
make a proper matrix-oriented version.

Basically, I start by precomputing a NxN matrix of the cosine term
cos( pi * (2*x - 1) * (u - 1) / (2*n) ), varying x and u along each
axis. The plan is to then multiply my DCT input to each row of the
precomputed cosines, which'll get me the summation terms I need, then
sum along the opposite axis and finally multiply by precomputed
weights.

I'm having a bit of trouble coming up with a good way to multiply the
DCT input with the precomputed matrix, however. The only way I've
managed to do it so far is using a loop. Here's what I have:

function at = DCT2( a )

    if ~isa(a, 'double')
        a = double(a);
    end

    s = size(a);

    wgt = cell(1, numel(s));
    precos = cell(1, numel(s));

    for i=1:numel(s)
        n = s(i);

        wgt{i}(1:n) = sqrt(2 / n);
        wgt{i}(1) = wgt{i}(1) / sqrt(2);
        precos{i} = bsxfun(@(a,b) cos( pi * (2*b - 1) * (a - 1) /
(2*n) ), (1:n), (1:n).');
    end

    for j = 1:s(2)
        at(:,j) = wgt{1} .* sum(bsxfun(@times, precos{1}, a(:,j)));
    end
end

I realize this function is riddled with issues which mostly stem from
the fact that I was initially trying to support 2D before I even got
1D to work. I won't pretend to ask anyone to do my schoolwork for me,
so my actual question is as follows!

How would I manage to replicate that last loop of functionality with
matrix notation?

I thought about removing the subscripts altogether:
at(:) = wgt{1} .* sum(bsxfun(@times, precos{1}, a(:)));
without a loop, and given a proper function I think it'd work on a 1D
input in theory. But since I'm going to expect 2D input to be passed,
I want to treat it as 1D on a row by row basis so I can basically do
transform = mydct(mydct(image).').';

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com