# Integration of a joint pdf with for loop?

2 views (last 30 days)
Mk on 2 Feb 2022
Commented: Alan Stevens on 2 Feb 2022
Hi, everyone
I want to Integration of a joint pdf which is square matrix using for loop, But i have error
"Index in position 2 is invalid. Array indices must be positive integers or logical values"
clc;
clear;
close all;
%%
x = linspace(0,1,100);
y = linspace(0,1,100);
[x y] = meshgrid(x,y);
fun = (6/5).*(x + y.^2);
dx = 1/length(x);
f = zeros(size(x));
fz = zeros(size(x));
for k = 1:2*length(x)% w = u + v;
for i = 1:length(x) %u
for j = 1:length(x) %v
if 0<=k-i && k-i<=100
f(i,j) = fun(i,k-i);
end
end
fz(k,:) = fz + trapz(f(:,i))*dx;
end
end

Alan Stevens on 2 Feb 2022
When k = 1and i = 1 then k-i = 0, therefore the second index in fun(i,k-i) is 0, but indices must be positive integers in Matlab.
##### 2 CommentsShowHide 1 older comment
Alan Stevens on 2 Feb 2022
There are lots of other values for which k=i, so k-i = 0 (e.g when k = 2, you also get to i = 2). You need to rethink the logic!

### Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!

Translated by