I have two matrices first one is:
test = [5;6;0;-1;0;5;0;6;0;8];
and the second one is:
test5 = [2;6;8;-1;0;7;8;6;8;8];
how to generate third matrix which is the result after the condition (if statment)...
the condition is if the value of test is equal 0 then the value of the new matrix is 0 , else if the value of the first matrix isn't equal 0 do some calculations on the second matrix which is test5 like (test5*7+5).
so the third matrix values depends on the two matrix before...

 채택된 답변

Stanislao Pinzón
Stanislao Pinzón 2020년 5월 17일

0 개 추천

Maybe like this?
test = [5;6;0;-1;0;5;0;6;0;8]+2;
test5 = [2;6;8;-1;0;7;8;6;8;8];
if ismember(0,test)
Matrix3 = 0;
else
Matrix3 = test5*7+5;
end

댓글 수: 7

This assuming that "if the value of test is equal 0" means you need to check if any of the elements of test is 0.
Matrix3 =
19
47
61
-2
5
54
61
47
61
61
but it doesn't take zero in consideration, it should be
Matrix3 =
19
47
0
-2
0
54
0
47
0
61
Okay, What I really need is checking if test equal zero or not:
if yes(test=0): then the value of test at this point is zero.
if no(test~=0): then do the calculations.
after that the 3rd matrix is combination of this condition.
so how to write a code like this?
ex:
test = [5;6;0;-1;0;5;0;6;0;8];
test5 = [2;6;8;-1;0;7;8;6;8;8];
calculate :Matrix3 = test5*7+5
Matrix3 = [ 19;47;0;-2; 0;54;0;47;0;61];
test = [5;6;0;-1;0;5;0;6;0;8];
test5 = [2;6;8;-1;0;7;8;6;8;8];
Matrix3 = zeros(length(test),1);
for i=1:length(test)
if test(i,1)==0
Matrix3(i,1) = 0;
else
Matrix3(i,1) = test5(i,1)*7+5;
end
end
or instead
test = [5;6;0;-1;0;5;0;6;0;8];
test5 = [2;6;8;-1;0;7;8;6;8;8];
Matrix3 = test5*7+5;
a = find(test==0);
Matrix3(a) = 0;
Stephen23
Stephen23 2020년 5월 17일
find is superfluous. Get rid of it.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Image Analyst
Image Analyst 2020년 5월 17일

1 개 추천

Try masking:
test = [5;6;0;-1;0;5;0;6;0;8];
test5 = [2;6;8;-1;0;7;8;6;8;8];
% Now multiply by 7 and add 5 only.
output = test5 * 7 + 5;
% Find indexes where test is zero.
mask = (test == 0)
% Erase where test was 0.
output(mask) = 0
Yundie Zhang
Yundie Zhang 2020년 5월 17일
편집: Walter Roberson 2020년 5월 17일

0 개 추천

test = [5;6;0;-1;0;5;0;6;0;8];
test5 = [2;6;8;-1;0;7;8;6;8;8];
if test ==0
newMAT = 0
elseif test ~=0
newMAT = (test5*7)+5
end

댓글 수: 2

Ibrahim AlZoubi
Ibrahim AlZoubi 2020년 5월 17일
편집: Ibrahim AlZoubi 2020년 5월 17일
the result is:
Undefined function or variable 'newMAT'.
when I want to know the new matrix "newMAT"
Remember that when you apply if or while to a non-scalar, that the result is only considered true if every item being tested is non-zero.
if test ==0
Only some of the items in test are 0, so that fails
elseif test ~=0
Only some of the items in test are non-zero, so that fails.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2020년 5월 17일

0 개 추천

Create the new matrix by applying the calculation to all of the entries in the second matrix, as if the rule about 0 was not present. This can be done in vectorized form in a single statement.
Now, everywhere that there is a 0 in the first matrix, replace the content of the third matrix with 0. This can be done in vectorized form in a single statement, using logical indexing.

댓글 수: 3

so you mean to edit the 3rd matrix manually ?
Stephen23
Stephen23 2020년 5월 17일
"so you mean to edit the 3rd matrix manually ?"
No. Use logical indexing:
which could be generated very simply using ==.
For example:
A = randi(10, 5, 8);
A(A>7) = -1;

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by