Index exceeds matrix dimensions

3 views (last 30 days)
Ryan
Ryan on 22 Oct 2012
I've got two matrices, Fluorescence <3840x15> and H <96x15>. I want to divide 40 Fluorescence values in each column by a value in the corresponding H column. Running the code as is results in a matrix J, but with just the first 40 rows of Fluorescence divided by the first row of H, instead of the <3840x15> matrix I wanted to end up with.
J = [];
K = [];
k = 1;
n = 1;
for i = 1:(r/40)
for j = 1:c
I = Fluorescence(k:(k+39),j) / H(n,j);
J = cat(2,J,I);
end
k = k + 40;
n = n + 1;
K = cat(1,K,J);
end
  1 Comment
Ryan
Ryan on 30 Oct 2012
fixed this by clearing J at the end of each iteration of i.
K = cat(1,K,J);
J = [];
end

Sign in to comment.

Answers (3)

Matt Fig
Matt Fig on 23 Oct 2012
Edited: Matt Fig on 23 Oct 2012
I think you mean this:
F = randi(100,3840,15); % Random 3840-by-15
H = randi(100,96,15); % Random 96-by-15;
J = F./expand(H,[40,1]);
Note that the EXPAND function is found on this page: EXPAND
Another way to do it without using the EXPAND function (with the same F and H as above):
J = zeros(3840,1);
J(1:40:end) = 1;
J = F./H(cumsum(J),:);

Image Analyst
Image Analyst on 23 Oct 2012
Do you have the Image Processing Toolbox? If you do, it's just two lines:
% Generate sample data.
Fluorescence = rand(3840, 15);
H = rand(96,15);
% Resize H to be the same size as Fluorescence.
H_matchingSize = imresize(H, size(Fluorescence), 'nearest');
% Divide them.
output = Fluorescence ./ H_matchingSize;
Or even one line:
output = Fluorescence ./ imresize(H, size(Fluorescence), 'nearest');
  1 Comment
Richard Brown
Richard Brown on 23 Oct 2012
or with a little more convoluted syntax, you can do it without the image processing toolbox
H_matching_size = interp1(1:96, H, reshape(repmat(1:96, 40, 1), 96*40, 1));
which is basically what imresize is doing

Sign in to comment.


Azzi Abdelmalek
Azzi Abdelmalek on 23 Oct 2012
Edited: Azzi Abdelmalek on 23 Oct 2012
out=Fluorescence (1:40,:)./H(1:40,:)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!