If statement without loop

조회 수: 14 (최근 30일)
Hokkien
Hokkien 2021년 12월 27일
댓글: Hokkien 2021년 12월 27일
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
DGM
DGM 2021년 12월 27일
What version are you using?
Hokkien
Hokkien 2021년 12월 27일
Hi I'm using Matlab R2020b

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

채택된 답변

Stephen23
Stephen23 2021년 12월 27일
편집: Stephen23 2021년 12월 27일
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
Hokkien
Hokkien 2021년 12월 27일
Thank you so much! I understand now!

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

추가 답변 (1개)

David Hill
David Hill 2021년 12월 27일
TR1_Ge70(TR1>TR1_Ge70(:,1))=0;
for k=1:size(A,2)
C(:,:,k)=A(:,k).*B;
end
  댓글 수: 1
Hokkien
Hokkien 2021년 12월 27일
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!

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by