Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to compare two matrices of different dimentions and get the result in new column

조회 수: 2 (최근 30일)
ShayanA
ShayanA 2019년 6월 13일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi everyone,
i wanne compare data in two column and get the result in a new column if my condition is true.
A = [2000;1950 ; 1980;2500 ; 2000 ; 5000 ; 2300 ;2000'];
B = [2;2;1;3;1;5;3;3 ; 2;3];
wanne go through all value in B and write in new column when they meet/ equal to 2000.
many thanks
/SA
  댓글 수: 2
Alex Mcaulley
Alex Mcaulley 2019년 6월 13일
The question is not clear to me. What is the expected result in your example?
Stephen23
Stephen23 2019년 6월 17일
ShayanA's "Answer" moved here:
Hi there,
actually wane go through two column (A & B )and find out all the data in B which is equal to 2000 in A.
then wanne show the result in new column. if not then NA or Zero in the new column.
hope i could specify better now :)
Thanks
SA

답변 (3개)

Fabio Freschi
Fabio Freschi 2019년 6월 17일
Try this
% find logical indices in A with 2000
idxLog = A == 2000;
% create the new vector with B values in idxLog and zeros in ~idxLog
Bnew = zeros(size(B));
Bnew(idxLog) = B(idxLog);
However it is still puzzling me why A and B have different dimensions

Andrei Bobrov
Andrei Bobrov 2019년 6월 17일
Bnew = B.*(A == 2000);

ShayanA
ShayanA 2019년 6월 18일
I appreciated Andrei & Fabio
Shayan

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by