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 个评论

Here's some help:
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
The code examples are not running due to a missing parenthesis.

请先登录,再进行评论。

回答(3 个)

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 个评论

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
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.
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!

请先登录,再进行评论。

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

1 个评论

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!

请先登录,再进行评论。

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 个评论

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!
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