using for loop for manipulating matrices

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

Image Analyst
Image Analyst 2011년 10월 13일
Here's some help:
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Jan
Jan 2011년 10월 13일
The code examples are not running due to a missing parenthesis.

댓글을 달려면 로그인하십시오.

답변 (3개)

Jan
Jan 2011년 10월 13일

0 개 추천

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

PChoppala
PChoppala 2011년 10월 13일
Well, no exactly, I want a 2-d matrix with rows = 2 and columns = 2xN, where N = no of columns in 'x'
In other words, for each column of x, we get a 2x2 matrix. I want all of them to be concatenated in 'h', so that h will be a 2x(2xN) = 2x4 matrix
PChoppala
PChoppala 2011년 10월 13일
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 2011년 10월 13일
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!

댓글을 달려면 로그인하십시오.

bym
bym 2011년 10월 13일

0 개 추천

to what Jan has provided add:
h = reshape(h,2,[]);
after the for loops

댓글 수: 1

PChoppala
PChoppala 2011년 10월 13일
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!

댓글을 달려면 로그인하십시오.

bym
bym 2011년 10월 14일

0 개 추천

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

PChoppala
PChoppala 2011년 10월 14일
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 2011년 10월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Calendar에 대해 자세히 알아보기

질문:

2011년 10월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by