hi, i have 2 different matrix with same dimensions and i need to know the min value against 2 values in the same position:
A=[ 0 1 3 5 6]
[2 3 5 8 9]
B=[2 2 4 7 8]
[3 5 7 9 10]
the algorithm confront the zero in first row first column of A with the 2 in first row first column of B and show the min value (0), so for the second row first column of each matrix and so on...i used a for cycle, but i want to know if i can try without any cycle.
In my cicle i use this:
for i 1:size(A)
find(A(1,:)<B(1,:)
end
how can i confront only element by element in the same position without any cycle?
i also have to use the elements of a matrix to do a scalar product using each element in a position and inserting it into a vector:
for i=1:length(A)
for j=1:N (size of rows)
ps1(i,j)=[A(i,j) 0]*[1 0]';
end
end
there is any way to do this without cycles?
i need the value in every position and that value is putted into a vector, then i make the scalar product with [1 0].
Tnx to all for the reply!

댓글 수: 1

Stephen23
Stephen23 2019년 9월 13일
See Matt J's answer for the simplest and most efficient solution.

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

 채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 13일
편집: KALYAN ACHARJYA 2019년 9월 13일

0 개 추천

"i have 2 different matrix with same dimensions and i need to know the min value against 2 values in the same position:"
A=randi(10,[2,5]) % Random 2x5 A Matrix
B=randi(10,[2,5]) % Random 2x5 B Matrix
result=(A<=B).*A+(A>B).*B

댓글 수: 8

Dario Riviezzo
Dario Riviezzo 2019년 9월 13일
wow it works!!! tnx a lot!
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 13일
My pleasure
Dario Riviezzo
Dario Riviezzo 2019년 9월 13일
if i have a matrix and a vector, can i sum the single elements of the vector on each row of a matrix without a cycle?
A=[ 0 1 3 5 6]
[2 3 5 8 9]
B[1 1 1 1 1]
C=[1 2 4 6 7]
[3 4 6 9 10]
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 13일
편집: KALYAN ACHARJYA 2019년 9월 13일
If the both matrices having same size (A and B)
A=[0 1 3 5 6;2 3 5 8 9]
[r c]=size(A)
B=ones(r,c)
C=A+B
If not (same size), one option to zero pad, then add.
Kalyan is missing the uniqueness of MATLAB , it’s simply:
A+B % bsxfun() prior to 2016b
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 9월 13일
Yes @Madhan
Dario Riviezzo
Dario Riviezzo 2019년 9월 13일
the second matrix (B) isn't made only of 1...i wrote that for an example, but it could be anything (with the same size row of A)
Dario Riviezzo
Dario Riviezzo 2019년 9월 13일
mmh thinking on this problem i can simply copy the same row to make a matrix with same dimension of the first, then simply add :)
u are teaching to me how to think on matrix!

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

추가 답변 (2개)

Matt J
Matt J 2019년 9월 13일

2 개 추천

min(A(1,:), B(1,:))
Dario Riviezzo
Dario Riviezzo 2019년 9월 13일

0 개 추천

still need this:
i also have to use the elements of a matrix to do a scalar product using each element in a position and inserting it into a vector:
for i=1:length(A)
for j=1:N (size of rows)
ps1(i,j)=[A(i,j) 0]*[1 0]';
end
end
there is any way to do this without cycles?
i need the value in every position and that value is putted into a vector, then i make the scalar product with [1 0].
Tnx to all for the reply!

댓글 수: 1

Matt J
Matt J 2019년 9월 13일
편집: Matt J 2019년 9월 13일
But ps1(i,j)=[A(i,j) 0]*[1 0]'; is the same as ps1(i,j)=A(i,j). Example,
>> [3,0]*[1,0]'
ans =
3
The whole loop is therefore equivalent to the trivial single command,
ps1=A;

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2019년 9월 13일

편집:

2019년 9월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by