using for loop for manipulating matrices
1 次查看(过去 30 天)
显示 更早的评论
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
2011-10-13
Here's some help:
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
回答(3 个)
Jan
2011-10-13
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.
bym
2011-10-14
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 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dynamic System Models 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!