If statement without loop

13 views (last 30 days)
Hokkien
Hokkien on 27 Dec 2021
Commented: Hokkien on 27 Dec 2021
Hi there are two questions I wish to ask:
First, I would like to use if else statement without the loop. Here is my attempt with the loop:
for k=1:15
for j=1:10
if A(j,k) > B(j,1), C(j,k)=0;
else
C(j,k)=A(j,k);
end
end
end
toc;
This took some computation time but I wish to cutdown the time. I attempt to use:
TR1_Ge70(TR>TR_Ge&)=0;
However, the matrix dimension is incorrect. Here is the example:
Matrix A Matrix B Matrix C
1 2 3 4 5 3 1 2 0 0 0
1 2 3 4 5 2 1 0 0 0 0
1 2 3 4 5 1 0 0 0 0 0
1 2 3 4 5 5 1 2 3 4 0
The second question will be bit massive and I fail to write one. I have a matrix A, let say 3 by 3. I also have matrix B, let say 3 by 3 as well. I wish to multiple the first column of matrix A with the entire matrix B, and the scond column and etc. For example:
Matrix A Matrix B Outcome using first column A: Outcome using second column B:
1 2 2 2 2 1 2 2 1 4 4 2
3 3 2 3 3 2 9 9 6 9 9 6
4 2 3 1 2 1 4 8 4 2 4 2
Based on this, I will get three set outcomes. However, it's seem like I cannot write the code and give me three different outcomes directly. The only way that I think is that I have to maintain only single column of Matrix A, and change it every time in order to get three different outcomes. Is it a quick way to do this? Thank you so much for the help!
  2 Comments
DGM
DGM on 27 Dec 2021
What version are you using?
Hokkien
Hokkien on 27 Dec 2021
Hi I'm using Matlab R2020b

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 27 Dec 2021
Edited: Stephen23 on 27 Dec 2021
A = repmat(1:5,4,1)
A = 4×5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
B = [3;2;1;5]
B = 4×1
3 2 1 5
C = A;
C((1:5)>=B) = 0
C = 4×5
1 2 0 0 0 1 0 0 0 0 0 0 0 0 0 1 2 3 4 0
  1 Comment
Hokkien
Hokkien on 27 Dec 2021
Thank you so much! I understand now!

Sign in to comment.

More Answers (1)

David Hill
David Hill on 27 Dec 2021
TR1_Ge70(TR1>TR1_Ge70(:,1))=0;
for k=1:size(A,2)
C(:,:,k)=A(:,k).*B;
end
  1 Comment
Hokkien
Hokkien on 27 Dec 2021
Hi Thank you so much, unfornutately I still have dimension problem. I'm sorry if my question is not clear. I have re-edit it with the example:
Matrix A Matrix B Matrix C
1 2 3 4 5 3 1 2 0 0 0
1 2 3 4 5 2 1 0 0 0 0
1 2 3 4 5 1 0 0 0 0 0
1 2 3 4 5 5 1 2 3 4 0
Thank you!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!