From: Jim on
Hi,

My output is as follows:

z =

-4.22033898305085
-5.94915254237288
-21.5084745762712


z =

-6.88607594936709
-10.1898734177215
-39.9240506329114


z =

-10.2888888888889
-14.6222222222222
-53.6222222222222


However, I wish the output to be:

z =

-4.22033898305085
-10.1898734177215
-53.6222222222222

How can i achieve this?

Below is my code.

%====================================
clc;
clear all;

V = [2 5 6 11;...
8 7 9 15 ;
6 4 36 55]

X = [34 87 65;...
59 79 45;...
45 22 73]

for idx= 1 : 1:size(V(:,3))

z = myfunc_answer2(V(:,3), X(:,mod(idx-1,size(X,2))+1))
%=====================================

end

Jimmy
From: someone on
"Jim " <iwonder26(a)gmail.com> wrote in message <i3n8hk$bu$1(a)fred.mathworks.com>...
> Hi,
>
> My output is as follows:
>
> z =
>
> -4.22033898305085
> -5.94915254237288
> -21.5084745762712
>
>
> z =
>
> -6.88607594936709
> -10.1898734177215
> -39.9240506329114
>
>
> z =
>
> -10.2888888888889
> -14.6222222222222
> -53.6222222222222
>
>
> However, I wish the output to be:
>
> z =
>
> -4.22033898305085
> -10.1898734177215
> -53.6222222222222
>
> How can i achieve this?
>
> Below is my code.
>
> %====================================
> clc;
> clear all;
>
> V = [2 5 6 11;...
> 8 7 9 15 ;
> 6 4 36 55]
>
> X = [34 87 65;...
> 59 79 45;...
> 45 22 73]
>
> for idx= 1 : 1:size(V(:,3))
>
> z = myfunc_answer2(V(:,3), X(:,mod(idx-1,size(X,2))+1))
> %=====================================
>
> end
>
> Jimmy

% Without knowing what is in myfunc_answer2,
% one inefficient solution is to change:
for idx= 1 : 1:size(V(:,3))

z = myfunc_answer2(V(:,3), X(:,mod(idx-1,size(X,2))+1))
%=====================================

end
% to:
for idx= 1 : 1:size(V(:,3))

temp = myfunc_answer2(V(:,3), X(:,mod(idx-1,size(X,2))+1));
%=====================================
z(idx) = temp(idx);
end
z
From: Jim on
hi,

thanks and sorry, "myfuc_answer2" is below.

And can you please let me know how is your approach inefficient and what can I do to increase efficiency?

%======================================
function returnValue = myfunc_answer2(maxOrMins, X)
returnValue = -(maxOrMins .* X(1) + X(3)) / X(2);
%======================================
From: someone on
"Jim " <iwonder26(a)gmail.com> wrote in message <i3naet$rgb$1(a)fred.mathworks.com>...
> hi,
>
> thanks and sorry, "myfuc_answer2" is below.
>
> And can you please let me know how is your approach inefficient and what can I do to increase efficiency?
>
> %======================================
> function returnValue = myfunc_answer2(maxOrMins, X)
> returnValue = -(maxOrMins .* X(1) + X(3)) / X(2);
> %======================================

% For the short code you have, the inefficiency is unnoticable.
% But if you think about it, with each iteration of the for loop
% you are computing 3 values, but only using one of them.
% so, in your original code,(without changing myfunc_answer2) replace:

for idx= 1 : 1:size(V(:,3))

z = myfunc_answer2(V(:,3), X(:,mod(idx-1,size(X,2))+1));
%=====================================

end

% with:

z = zeros(3,1); % preallocate z
for idx= 1 : 1:size(V(:,3))

z(idx) = myfunc_answer2(V((idx,3), X(:,mod(idx-1,size(X,2))+1));
%=====================================

end

z
 | 
Pages: 1
Prev: doubt
Next: plotting a 3D data in plane by color