Problem with double use of a for loop?

I have a problem aith a code. I have 2 matrices , A and B. Matrix A has 3x3 dimensions, while matrix B has 1x3 dimensions. I would like to minus (remove with "-"), the (1,1) number of B from each one number (each line) of the 1st column of A matrix.
After that, I I would like to minus (remove with "-"), the (1,2) number of B from each one number (each line) of the 2nd column of A matrix.
Last, I would like to minus (remove with "-"), the (1,3) number of B from each one number (each line) of the 3rd column of A matrix.
I tried these commands:
ndata=size(A,1)
[rows columns] = size(B);
for jj=1:size(A,1);
for zz=(columns);
MINUS(jj)=(A(:,jj))- B(1,zz);
end
end
but no use. Could you please help me?

댓글 수: 5

M = A-B % why do you need to use nested loops?
Ivan Mich
Ivan Mich 2022년 11월 13일
Because I want to have a result of 3x3 matrix and I think that loop is neccessary for my purpose..
Stephen23
Stephen23 2022년 11월 13일
편집: Stephen23 2022년 11월 14일
"Because I want to have a result of 3x3 matrix and I think that loop is neccessary for my purpose"
It works for me without loops, using arrays of the sizes that you gave in your question:
A = randi(9,3,3)
A = 3×3
9 6 8 5 7 2 1 8 7
B = randi(9,1,3)
B = 1×3
9 8 3
C = A-B
C = 3×3
0 -2 5 -4 -1 -1 -8 0 4
It is unclear what you expect to gain using nested FOR loops.
Steven Lord
Steven Lord 2022년 11월 14일
One potential reason to use a for loop would be if the poster is using an older release of MATLAB, one that predates the introduction of implicit expansion. Another is if this is a homework assignment and the professor or teacher that assigned the work forbade the students from using implicit expansion.
Stephen23
Stephen23 2022년 11월 14일
"One potential reason to use a for loop would be if the poster is using an older release of MATLAB, one that predates the introduction of implicit expansion"

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2022년 11월 13일

댓글:

2022년 11월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by