MATLAB: How to check if a matrix has a certain variable

matrixmatrix arraymatrix manipulation

Say I have matrix X:
X = [ 1 , 4, 2]
where
X(1) = 1
X(2) = 4
X(3) = 2
and matrix A with:
A(1) = X(1) + X(2)
A(2) = X(2) + X(3)
A(3) = X(1) + X(3)
I made a nested for loop j from 1 to 3 and k from 1 to 3 to make a 3×3 matrix as my output as matrix W
W = zeros(3,3)
for j = 1:3
for k = 1:3
if (don't know what to put here)
W(j,k) = A(j) + 100
else
W(j,k) = A(j)
end
W(j,k) = A(j)
end
end
j is for each row corresponding to A
k is for each column corresponding to X
In each element of the matrix,I want to check each A(j) in the matrix if it has X(k) in its equation then if true I would add the value 100 to that A(j) that was true.
And if that A(j) did not contain X(k) in its equation then nothing would be added to A(j).
Then I would insert the new value to replace the old element in the matrix
For example, if if the nested for loop goes to W(1,2) then it would check if X(2) is in A(1).
Since true then new W(1,2) would be A(1)+100
My problem is that I don't know how to code to check for each X(k) in A(j)
How do I check A(j) for X(k) as a variable?

Best Answer

Rewrite A as
M = [1 1 0;
0 1 1;
1 0 1];
A = M * X';
Now you can test if A(j) uses X(k) by testing whether M(j, k) is nonzero.
Related Question