Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Elementwise Expansion: How to "Dilute" a Matrix

Subject: Elementwise Expansion: How to "Dilute" a Matrix

From: cartonn

Date: 25 Jul, 2011 22:38:09

Message: 1 of 7

I'm struggling doing this without using loops. I'm kind of new to MATLAB, and I was told that I'm not supposed to use loops, if possible.

So here's what I want to do:

T = [4 7]
      [3 0]

dilution_size = [2 3]

dilute(T, dilution_size) = [4 4 4 7 7 7]
                                   [4 4 4 7 7 7]
                                   [3 3 3 0 0 0]
                                   [3 3 3 0 0 0]

So I want to repmat each element of the matrix by the given dimension.

Thanks for your help!

Subject: Elementwise Expansion: How to "Dilute" a Matrix

From: cartonn

Date: 25 Jul, 2011 23:02:09

Message: 2 of 7

I guess this can also be considered "zooming" into a matrix. I'm not sure, but some image processing functions may be useful... But I'm not familiar with any of them.

Subject: Elementwise Expansion: How to "Dilute" a Matrix

From: Roger Stafford

Date: 25 Jul, 2011 23:13:08

Message: 3 of 7

"cartonn " <cartonn30gel@yahoo.com> wrote in message <j0kr8h$enn$1@newscl01ah.mathworks.com>...
> I'm struggling doing this without using loops. I'm kind of new to MATLAB, and I was told that I'm not supposed to use loops, if possible.
>
> So here's what I want to do:
>
> T = [4 7]
> [3 0]
>
> dilution_size = [2 3]
>
> dilute(T, dilution_size) = [4 4 4 7 7 7]
> [4 4 4 7 7 7]
> [3 3 3 0 0 0]
> [3 3 3 0 0 0]
>
> So I want to repmat each element of the matrix by the given dimension.
>
> Thanks for your help!
- - - - - - - - - -
 D = kron(T,ones(d(1),d(2)));

where d = dilution_size and D = dilute.

  Note: I would suggest not taking that advice too seriously about avoiding loops. Sometimes they furnish the very best way of carrying out a task. What counts in execution speed most of all is a wise selection of algorithm, regardless of whether or not it requires the use of for-loops or while-loops.

Roger Stafford

Subject: Elementwise Expansion: How to "Dilute" a Matrix

From: cartonn

Date: 25 Jul, 2011 23:25:09

Message: 4 of 7

Thank you!! I wouldn't have been able to figure this out on my own. Also, I know about the kronecker tensor product now.

And thanks for the advice on loops. I just assume that if there's way to do something using MATLAB functions (which I'm assuming are already optimized), it's probably in my best interest to use them rather than trying to write my own functions.

Subject: Elementwise Expansion: How to "Dilute" a Matrix

From: Roger Stafford

Date: 26 Jul, 2011 00:45:11

Message: 5 of 7

"cartonn " <cartonn30gel@yahoo.com> wrote in message <j0ku0l$l9p$1@newscl01ah.mathworks.com>...
> ..... I just assume that if there's way to do something using MATLAB functions (which I'm assuming are already optimized), it's probably in my best interest to use them .......
- - - - - - - - - -
  That's almost always true if a single matlab function is designed to solve just your problem. However, when a task involves processing that no single matlab function has been designed for, it is in my opinion a mistake to rule out possible solutions involving for-loops or while-loops in favor of only "vectorized" methods involving some combination of matlab functions. As I say, sometimes, though certainly not always, the loop solutions are better than such combinations. It is part of the programmer's job to decide which is best to use. The 'tic' and 'toc' functions are an excellent tool to use in making such decisions.

Roger Stafford

Subject: Elementwise Expansion: How to "Dilute" a Matrix

From: ImageAnalyst

Date: 26 Jul, 2011 01:05:55

Message: 6 of 7

And, just for illustrative purposes, I'll supply my code of an example
where for loop is much much faster than and alternative "vectorized"
version:



% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.
% Discussed in newgroup thread
% http://www.mathworks.com/matlabcentral/newsreader/view_thread/305371

clc;
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.

% Initialize an array.
myArray = zeros (512);

% Generate a list of 1000 random coordinates
% that we will assign a value of 1 to.
randomCoords = int32(randi(512, [1000 2]));
rows = randomCoords(:,1);
cols = randomCoords(:,2);

% First assign values using for loop.
% Start timer
tStart1 = tic;
for k = 1 : size(randomCoords, 1)
% Set each point one at a time.
myArray(rows(k), cols(k))=1;
end
tElapsed1 = toc(tStart1);
fprintf('The time for the "for" loop is %f\n', tElapsed1);
% Note the short time.

% Now do again, this time timing the assignment where we
% first get linear indexes using sub2ind,
% instead of using a for loop.
tStart2 = tic;
linearIndexes = sub2ind([512 512], rows, cols);
myArray(linearIndexes) = 1;
tElapsed2 = toc(tStart2);
fprintf('The time using sub2ind is %f, ', tElapsed2);
fprintf('a factor of %f longer\n', tElapsed2/tElapsed1);
% Note the time of sub2ind is about 4-10 times as long
% as it was using the for loop.

Subject: Elementwise Expansion: How to "Dilute" a Matrix

From: Bruno Luong

Date: 26 Jul, 2011 07:09:08

Message: 7 of 7

"cartonn " <cartonn30gel@yahoo.com> wrote in message <j0ku0l$l9p$1@newscl01ah.mathworks.com>...
>
> And thanks for the advice on loops. I just assume that if there's way to do something using MATLAB functions (which I'm assuming are already optimized).

More or less. For example in the specific case of KRON, there are two FEX submissions that improve the speed over Matlab builtin KRON function.

Bruno

Tags for this Thread

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.

Contact us