Hi
Simple question....
I have an output 'P' which is a 10X10 matrix. I start with
for i=1:10
for j=1:10
P(i,j) = %some operation that gives the answer at each i,j
end
end
I use 'surfl' to plot P.
Now I have
for i=1:0.5:10
for j=1:0.5:10
P(i,j) = %some operation that gives the answer at each i,j
end
end
Then, P will still be a 20X20 matrix, but its now plotting on surf, because I think it takes only integers.
So, I used
c1=1;
for i=1:0.5:10
c2=1;
for j=1:0.5:10
P(c1,c2) = %some operation that gives the answer at each i,j
c2=c2+1;
end
c1=c1+1;
end
surf(P)
This gives me a figure for a 20X20 matrix I want, but its mentioned as 1:20 and 1:20 on x & y-axes, while I want it to depict 1:10 and 1:10....
Please advice what I need to change to get that!!!

2 个评论

http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Whoops, I never knew about this. thanks Walter

请先登录,再进行评论。

 采纳的回答

xv = 1 : 0.5 : 10;
yv = 1 : 0.5 : 10;
for i = 1 : length(xv)
for j = 1 : length(yv)
P(i,j) = %some operation that gives the answer at each xv(i), yv(j)
end
end
surf(xv, yv, P)

7 个评论

I am sorry, I assumed it would work and accepted your answer, but 'no'.
I get the error
*Subscript indices must either be real positive integers or logicals.*
*Error in MUSIC_ifft (line 166)
P(xv,yv) = (a_r * a_r');*
I think that is because P takes only integer values of its locations.
Can you suggest another method???
I used
_c1=1;
_for i = 1:0.5:10
_ c2=1;
_ for j = 1:0.5:10
_ %statements
_ P(xv,yv) = (a_r * a_r');
_ c2=c2+1;
_ end
_ c1=c1+1;
_ end
_ surfl(1:0.5:10,1:0.5:10,20*log10(P));
But I think there should be another simpler way. Some other form of surf or plot that makes assignment and plotting simpler!!!
Please advice
Cheer
I used the underscore and asterisk, but letter did not bold or go italic!!!
Sorry, markup cannot be used in comments. Markup in comments is the highest-rated item on the wish-list !
Notice in my version of the code, I assigned to P(i,j), whereas you assigned to P(xv,yv) . Assigning to (i,j) is correct. Notice too that I wrote in the comment that the operation should give the answer at xv(i) and yv(j) -- the indexed values are the "real" values to use, whereas i and j are the counters.
Oh, but I need each of 1,1.5,2,2.5...10 in side my loop.
The sample code you posted does not need the complete list of values inside the loop: only the _current_ values. The current x is xv(i) and the current y is yv(j)
Whoops, sorry then, I need them.
for i=1:0.5:10
for j=1:0.5:10
d_r = sqrt(sum(([1 8 ; 0 7]-repmat([i11;j11],1,10)).^2));
% other statements that process d_r and give a_r
P(i,j) = (a_r * a_r');
end
end
So, P is now a 20X20 matrix but has calculated the algorithm in the range space of 10X10. So i want the surf command to show me only 10X10 on its x and y axes.
Please advice
Cheers
If you are going to insist on using for i=1:0.5:10 then just use your code from your first comment, above, where you use the c1 and c2 counters. It isn't ideal code, but you are stuck on the idea that i and j must be the values instead of being allowed to index the values, so you might as well stick with what you understand.
-----
Perhaps this would make things more clear:
xv = 1 : 0.5 : 10;
yv = 1 : 0.5 : 10;
for xidx = 1 : length(xv)
i = xv(xidx);
for yidx = 1 : length(yv)
j = yv(yidx);
P(xidx,yidx) = %some operation that gives the answer at each i, j
end
end
surf(xv, yv, P)
Then you can have clear indexes _and_ you can still use "i" and "j" as the names of your variables that hold the current values.
-----
In your code, you might want to clarify what i11 and j11 are. Your d_r appears to be calculating the same value for all i and j combinations; if so then it would be more efficient to calculate it once outside the loop.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Graphics Performance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by