Error in MATLAB's plot function [closed] - matlab

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this function
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
where thDesnorm is:
[-23.6608
0.1919
0.1866]
When i want to plot this function this way:
domain = 0:1:100;
figure;
plot(domain, h45(domain));
I get this error:
Error using *
Inner matrix dimensions must agree.
Error in #(x)1/(1+exp(-thDesnorm'*[1,45,x]'))
This function works when calling it with, for example, h45(1).
I guess than in plotting, the function is receiving all the domain vector as the parameter x, and not one by one the values of this vector.

As you guessed, the parameter x is your full array domain. This obviously causes a dimensional error in the dot product thDesnorm'*[1,45,x]. A quick fix would be using arrayfun to evaluate h45. For instance:
thDesnorm = [-23.6608
0.1919
0.1866];
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
domain = 0:1:100;
figure;
plot(domain, arrayfun(#(x)h45(x),domain)) % See modification in h45 call

Related

How to define a function with a matrix [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to define a two variables function g so that g(x,y) be a 2*2 matrix. To do this, I define g(x,y)=[1,1;x,y] but when I put g(1,1) I don't get any answer. How can I evaluate to g?
The code g(x,y)=[1,1;x,y] itself will not do anything. I assume that your expect result will be g=[1,1,1,1]? Therefore you should do as follow:
g=g_func(1,1);
disp(g)
function g=g_func(x,y)
g=[1,1;x,y];
end
It's not that different from the previous answer, but perhaps an anonymous function would meet your needs:
>> g = #(x,y)[1,1;x,y];
>> g(5,6)
ans =
1 1
5 6
Alternatively, if you want g to accept only one input (i.e. a 2-element vector, instead of two scalars), you could do:
g = #(x)[1,1;x(1),x(2)];
% or
g = #(x)[1,1;x(:).'];

Failing show legend [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a huge amount of data to plot, and thanks to:
x=matlab.lang.makeValidName(strcat(...));
assignin('base',x,I);
for each loop of process, where I calculate I, I assign the value of I (a vector) to the variable name I_Pnum1_Bnum2 where num1 is the value of P and num2 the value of B. So, at the end I have lots of I's for:
num1=-4:-1:-14;
num2=[0 5 10 20:20:120 150 170 200 220];
That's why, for each value of P, I want to plot (on the same graph) all I's for different B:
num1=-4:-1:-14;
num2=[0 5 10 20:20:120 150 170 200 220];
for i=1:length(num1)
legend=[];
figure(i)
for j=1:length(num2)
Y=matlab.lang.makeValidName(strcat('I_p',num2str(abs(num1(i))),'_B',num2str(double(num2(j)))));
plot(V,eval(Y),'linewidth',2)
hold on
leg=strcat("B= ",num2str(b(j)));
legend=[legend leg];
end
title(strcat("Caractéristiques I(V) #",num2str(p(i)),"dBm"))
legend(legend);
end
clc;
The problem: I get
Function 'subsindex' is not defined for values of class 'string'.
and it is due to the line legend(legende), and I don't understand why, because the vector legende is well defined.
The error occurs because of a conflict between your variable named legend and the build-in MATLAB function legend(). Rename your variable to e.g. leg1 then it should work as expected.

Matlab: How do you implement this sum? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to implement the following:
x_i = e ^ (-1 - sum (y_j * A_ji))
where i = 1..10, j = 1..5 and A is a 5x10 matrix (randomly generated).
I tried using symsum but it gave me an index error. Could someone please help me figure out how to implement this?
With
A = rand(5,10); %# random 5x10 array
y = rand(1,5); %# random 1x5 array
Your sum becomes
x = exp( -1 - y*A);
thanks to linear algebra.

need help about solving differential equation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have tried to solve this following program in matlab but failed.
clear all
syms y(x)
y=dsolve(2.5e-3*diff(y, 2) + 0.5*diff(y) +122.5*y == 2570);
y=0 , y=20 ;
I want to find value of y.
Assuming this is not simply a copy-past error, you need to specify the location of the boundary values (or potentially initial conditions). The easiest way to do this is within the call to dsolve itself (since I don't know the locations, I'm going to assume y = 0 at x = 0 and y = 20 at x = 1/50):
syms x y(x)
xa = sym(0);
xb = sym('1/50');
y(x) = dsolve(2.5e-3*diff(y, 2) + 0.5*diff(y) + 122.5*y == 2570,y(xa)==0 , y(xb)==20)

Why is my for loop running over? (Matlab) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have the following code:
for i = 1:RGB_size(2) % RGB_size(2) = 1296 //X coords
for j = 1:1:RGB_size(1) % RGB_size(1) = 964 //Y coords
if mask(i,j) == 1
data(next_pixel,:) = [ImgIndex, ImgTake, i, j, RGB(i,j,1), RGB(i,j,2), RGB(i,j,3),...
HSIR(i,j,1), HSIR(i,j,2), HSIR(i,j,3)];
next_pixel = next_pixel + 1; %get next pixel
end
end
end
But Matlab won't run my code because it says I'm trying to access mask(965,1) and my variable mask size is 1296×964. However, I don't see how this is possible. Any thoughts?
RGB_size is calculated from a variable called RGB, which is the same variable used to create the variable mask. I have verified that they are both the same size with the debugger.
Actual error message is:
Attempted to access mask(965,1); index out of bounds because size(mask)=[964,1296].
You have switched the i and j index of the mask.