Filter löschen
Filter löschen

how to generate new matrix with if statment

5 Ansichten (letzte 30 Tage)
Ibrahim AlZoubi
Ibrahim AlZoubi am 17 Mai 2020
Kommentiert: Walter Roberson am 17 Mai 2020
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...

Akzeptierte Antwort

Stanislao Pinzón
Stanislao Pinzón am 17 Mai 2020
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 Kommentare
Stanislao Pinzón
Stanislao Pinzón am 17 Mai 2020
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 am 17 Mai 2020
find is superfluous. Get rid of it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Image Analyst
Image Analyst am 17 Mai 2020
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 am 17 Mai 2020
Bearbeitet: Walter Roberson am 17 Mai 2020
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 Kommentare
Ibrahim AlZoubi
Ibrahim AlZoubi am 17 Mai 2020
Bearbeitet: Ibrahim AlZoubi am 17 Mai 2020
the result is:
Undefined function or variable 'newMAT'.
when I want to know the new matrix "newMAT"
Walter Roberson
Walter Roberson am 17 Mai 2020
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.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 17 Mai 2020
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 Kommentare
Stephen23
Stephen23 am 17 Mai 2020
"so you mean to edit the 3rd matrix manually ?"
No. Use logical indexing:
which could be generated very simply using ==.
Walter Roberson
Walter Roberson am 17 Mai 2020
For example:
A = randi(10, 5, 8);
A(A>7) = -1;

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Assessments, Criteria, and Verification finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by