How to ignore an element(s) in a vector or matrix

조회 수: 33 (최근 30일)
Sam Mahdi
Sam Mahdi 2019년 8월 19일
답변: Chris 2019년 8월 19일
Hello everyone,
I'm attempting to run a function, but ignore a certain element within my vector. I've looked online, but most of the answers actually outright remove the element(s) in question, rather than having the function ignore them.
Here is the basic setup:
X = [ones(3,1) magic(3)];
y = [1 0 1]';
theta = [-2 -1 1 2]';
with my equations:
h=sigmoid(X*theta);
% where sigmoid is simply 1/(1+e^-(stuff in parenthesis))
sum_values=(log(h')*(-y)-((log(1-h')*(1-y))));
J=(1/m)*sum_values+(lambda/(2*m))*(sum(theta).^2);
grad=((1/m)*((h-y)'*X)')+(lambda/m)*(theta);
Now in the above setup, I don't want to use theta0 values (i.e. theta(1,1) and X(:,1))
My original solution was to outright remove it:
b=theta;
b(1)=[];
c=X;
c(:,1)=[];
J=(1/m)*sum_values+(lambda/(2*m))*(sum(b'.^2));
grad=((1/m)*((h-y)'*c)')+(lambda/m)*(b);
This doesn't work however, because the function is assuming you are using the original size of X and theta (and I am now using b and c, which are different size vectors and matrices). So what I want to do is simply ignore the certain elements within my vector and matrix, but I don't want to eliminate them (i.e. create a new differently sized matrix/vector). All the stuff I've seen so far eliminate the actual value using the same logic as the above technique I tried.

채택된 답변

Chris
Chris 2019년 8월 19일
You can specify a sub compontent of a matrix but the resulting matrix math needs to remain logical
>> theta = [1,2,3,4]; alpha = [5,6,7,8];
>> theta(2:4) .* alpha(2:4)
ans =
12 21 32
>> theta(2:4) * alpha(2:4)'
ans =
65
theta(2:4) * alpha(2:4)
Error using * ...

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by