

I have a small problem with the syntax.
I have a function "frame" which I don't want to change since it is used in this form in other functions:
function fr = frame (x)
fr=x(1,:)+x(2,:);
endfunction
Now, when I want to plot the function as surface, I tried:
x1=0:0.1:1;
[X,Y]=meshgrid(x1,x1);
Z=frame([X,Y]); #< not ok I tried different variants but all do not work
surf(X,Y,Z); #< error message
How do I have to modify the line "Z=frame([X,Y])" that the surf command works and gives the surface?
Thanks
Karl
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


karl wrote
I have a small problem with the syntax.
...
How do I have to modify the line "Z=frame([X,Y])" that the surf command works and gives the surface?
X and Y are matrices (in your case 11x11) and [X,Y] is even 11x(2*11). Your functions expects vectors and thus it does not work properly. You either have to modify "frame" to allow matrix input or you have to loop over the entries. Here are three possibilities:
function karl
x1=0:0.1:1;
[X,Y]=meshgrid(x1,x1);
Z=frame_modified(X,Y);
figure;
surf(X,Y,Z);
for i=1:size(Z,1)
Z(i,:)=frame([X(i,:); Y(i,:)]);
end
figure;
surf(X,Y,Z);
for i=1:size(Z,1)
for j=1:size(Z,1)
Z(i,j)=frame([X(i,j); Y(i,j)]);
end
end
figure;
surf(X,Y,Z);
end
function fr = frame_modified (x,y)
fr=x+y;
endfunction
function fr = frame (x)
fr=x(1,:)+x(2,:);
endfunction


Am 12.11.2015 um 00:26 schrieb Sebastian Schöps:
> karl wrote
>> I have a small problem with the syntax.
>> ...
>> How do I have to modify the line "Z=frame([X,Y])" that the surf command
>> works and gives the surface?
> X and Y are matrices (in your case 11x11) and [X,Y] is even 11x(2*11). Your
> functions expects vectors and thus it does not work properly. You either
> have to modify "frame" to allow matrix input or you have to loop over the
> entries. Here are three possibilities:
>
> function karl
> x1=0:0.1:1;
> [X,Y]=meshgrid(x1,x1);
>
> Z=frame_modified(X,Y);
> figure;
> surf(X,Y,Z);
>
> for i=1:size(Z,1)
> Z(i,:)=frame([X(i,:); Y(i,:)]);
> end
> figure;
> surf(X,Y,Z);
>
> for i=1:size(Z,1)
> for j=1:size(Z,1)
> Z(i,j)=frame([X(i,j); Y(i,j)]);
> end
> end
> figure;
> surf(X,Y,Z);
>
> end
>
> function fr = frame_modified (x,y)
> fr=x+y;
> endfunction
>
> function fr = frame (x)
> fr=x(1,:)+x(2,:);
> endfunction
>
>
>
> 
> View this message in context: http://octave.1599824.n4.nabble.com/Functionofvectorwithmeshgridandsurftp4673436p4673444.html> Sent from the Octave  General mailing list archive at Nabble.com.
>
> _______________________________________________
> Helpoctave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/helpoctaveHallo Sebastian thanks for the answer, BUT:
I can't modify frame as I wrote already, since then I have to change many other things, and I would like
to avoid looping. Let us consider a simpler case:
function fr = frame1 (x)
fr=x(1)+x(2);
endfunction
Is it possible in this case, i.e. with no modification of frame1, to avoid looping?
Ciao
Karl
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


karl wrote
function fr = frame1 (x)
fr=x(1)+x(2);
endfunction
Is it possible in this case, i.e. with no modification of frame1, to avoid looping?
No. You will always need two iterations to evaluate all combinations of x and y. However, these loops can be vecorized if your function allows it. In your case it seems that only one of the two loops can be vectorized.
Please consider that the input of your function is a 11by22MATRIX if you try to vectorize both loops. Thus the following does not make sense (it assumes that x is a 2by1 vector):
function fr = frame1 (x)
fr=x(1)+x(2);
endfunction
if you do not believe me, ask Octave:
function fr = frame1 (x)
size(x)
fr=x(1)+x(2);
endfunction


Am 12.11.2015 um 08:02 schrieb Sebastian Schöps:
> karl wrote
>> function fr = frame1 (x)
>> fr=x(1)+x(2);
>> endfunction
>>
>> Is it possible in this case, i.e. with no modification of frame1, to avoid
>> looping?
> No. You will always need two iterations to evaluate all combinations of x
> and y. However, these loops can be vecorized if your function allows it. In
> your case it seems that only one of the two loops can be vectorized.
>
> Please consider that the input of your function is a 11by22MATRIX if you
> try to vectorize both loops. Thus the following does not make sense (it
> assumes that x is a 2by1 vector):
>
> function fr = frame1 (x)
> fr=x(1)+x(2);
> endfunction
>
> if you do not believe me, ask Octave:
>
> function fr = frame1 (x)
> size(x)
> fr=x(1)+x(2);
> endfunction
>
>
>
> 
> View this message in context: http://octave.1599824.n4.nabble.com/Functionofvectorwithmeshgridandsurftp4673436p4673448.html> Sent from the Octave  General mailing list archive at Nabble.com.
>
> _______________________________________________
> Helpoctave mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/helpoctaveI have found now the solution. With "vec" one can transform the matrices X and Y into vectors, one can calcuulate the
function
values with one command and then getting the matrix form of the function values with "reshape".
_______________________________________________
Helpoctave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/helpoctave


karl wrote
I have found now the solution. With "vec" one can transform the matrices X and Y into vectors, one can calcuulate the
function
values with one command and then getting the matrix form of the function values with "reshape".
Nice solution. I feel actually ashamed that I did not think of it :)

