using for loop for manipulating matrices
1 view (last 30 days)
Show older comments
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
on 13 Oct 2011
Here's some help:
http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Answers (3)
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
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
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.
See Also
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!