using for loop for manipulating matrices

1 view (last 30 days)
PChoppala
PChoppala on 13 Oct 2011
Hello there, can anyone please help me out with this
Suppose, I have
x = [1,0.5,1,0.5,5]'
for i=1:2
for j=1:2
h(i,j) = x(5)/(2*pi*0.7^2) * exp(- ( (i-x(1))^2 +
j-x(3))^2 ) / (2*0.7^2) );
end
end
That will create a 2x2 matrix called 'h'.
Now I have
x = [1,0.5,1,0.5,5 ; 2,0.5,1,0.5,10]'
and I need to calculate h for each column of x, so h would be a 2x(2xN) matrix, where N is the number of columns in 'x'
Can any one help me how to get this?
I tried
for k=1:N
for i=1:2
for j=1:2
h(i,j) = x(5,k)/(2*pi*0.7^2) * exp(- ( (i-x(1,k))^2 +
j-x(3,k))^2 ) / (2*0.7^2) );
end
end
end
but it fails!
I tried
for k=1:N
for i=1:2
for j=1:2
h(i,j) = x(5,k)/(2*pi*0.7^2) * exp(- ( (i-x(1,k))^2 +
j-x(3,k))^2 ) / (2*0.7^2) );
end
end
h1 = horzcat(h)
end
and it fails!
Please help mates!
  2 Comments
Image Analyst
Image Analyst on 13 Oct 2011
Here's some help:
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Jan
Jan on 13 Oct 2011
The code examples are not running due to a missing parenthesis.

Sign in to comment.

Answers (3)

Jan
Jan on 13 Oct 2011
What is a "2x(2xN) matrix"? I assume, you want an [2 x 2 x N] array.
a = 2*pi*0.7^2;
b = 2*0.7^2;
h = zeros(2, 2, N); % Pre-allocate!
for k=1:N
for i=1:2
for j=1:2
h(i,j,k) = x(5,k)/a * exp(-((i-x(1,k))^2 + j-x(3,k))^2) / b ???)???;
end
end
end
Your code exampled contains a not matching parenthesis. I've included it in question marks.
  3 Comments
PChoppala
PChoppala on 13 Oct 2011
Here you go, the correct statement for 'h'
h(i,j) = x(5,k)/(2*pi*0.7^2) * exp(- ( (i-x(1,k))^2 + (j-x(3,k))^2 ) / (2*0.7^2) )
Matching parenthesis there! The last statement was not copied enough.
PChoppala
PChoppala on 13 Oct 2011
h = nan(2,(2*N))
x = [1,0.5,1,0.5,5 ; 2,0.5,1,0.5,10]'
for k=1:N
k
for i=1:2
i
for j=1:2
j
h(i,j) = x(5,k)/(2*pi*0.7^2) * exp(- ( (i-x(1,k))^2 + (j-x(3,k))^2 ) / (2*0.7^2) )
end
end
%h1 = horzcat(h)
end
that's the entire code I tried, but fails to get the desired result!

Sign in to comment.


bym
bym on 13 Oct 2011
to what Jan has provided add:
h = reshape(h,2,[]);
after the for loops
  1 Comment
PChoppala
PChoppala on 13 Oct 2011
h = nan(2,(2*N))
x = [1,0.5,1,0.5,5 ; 2,0.5,1,0.5,10]'
for k=1:N
k
for i=1:2
i
for j=1:2
j
h(i,j,k) = x(5,k)/(2*pi*0.7^2) * exp(- ( (i-x(1,k))^2 + (j-x(3,k))^2 ) / (2*0.7^2) )
end
end
%h1 = horzcat(h)
end
that's the entire code I tried, but fails to get the desired result!

Sign in to comment.


bym
bym on 14 Oct 2011
is this what you are after?
clc;clear
x = [1,0.5,1,0.5,5 ; 2,0.5,1,0.5,10]';
h = zeros(2,size(x,2));
for i=1:2
for j=1:2*size(x,2)
h(i,j) = x(5)/(2*pi*0.7^2)...
* exp(- ( (i-x(1))^2 + j-x(3))^2 )...
/ (2*0.7^2);
end
end
disp(h)
  2 Comments
PChoppala
PChoppala on 14 Oct 2011
Hi, I think I am almost there, but new doubts crept in when trying your code.
Using
x = [1,0.5,1,0.5,5 ; 2,0.5,1,0.5,10]'
hArr = []
for k=1:N
k
for i=1:2
i
for j=1:2
j
h(i,j) = x(5,k)/(2*pi*0.7^2) * exp(- ( (i-x(1,k))^2 + (j-x(3,k))^2 ) / (2*0.7^2) )
end
end
hArr = [hArr h]
end
gives
hArr =
1.6240 0.5854 1.1708 0.4220
0.5854 0.2110 3.2481 1.1708
and...using
clc;
clear
x = [1,0.5,1,0.5,5 ; 2,0.5,1,0.5,10]';
h = zeros(2,size(x,2));
for i=1:2
for j=1:2*size(x,2)
% h(i,j) = x(5)/(2*pi*0.7^2)...
% * exp(- ( (i-x(1))^2 + j-x(3))^2 )...
% / (2*0.7^2);
h(i,j) = (x(5)/(2*pi*0.7^2)) * exp(- (((i-x(1))^2) + ((j-x(3))^2)) / (2*0.7^2) )
end
end
disp(h)
gives
h=
1.6240 0.5854 0.0274 0.0002
0.5854 0.2110 0.0099 0.0001
You may as well try for each column of 'x' separately to see the correct answer.
Please tell me if my method, the first one is correct or not!
bym
bym on 14 Oct 2011
well, I think you would be in the best position to judge whether it is correct or not, since I have only a partial idea of what you want to accomplish. If you want the 2x2 output of a column to be concatenated horizontally, then I would say you have achieved this.

Sign in to comment.

Categories

Find more on Dynamic System Models 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!