I have a 84x7 matrices. On the entire column 2 and 4, they need to be subtracted from higher number - lower and loop through the whole 84 rows and put them into a new 84x1 vector. How can this be done? let 4x4 matrix a = [x 3 2 y; x 4 1 y; x 1 6 y; x 2 7 y; x 5 1 y;
b= [3-2 4-1 6-1 7-2 5-1]
b= [1 3 5 5 4 ]

 Akzeptierte Antwort

madhan ravi
madhan ravi am 21 Okt. 2018
Bearbeitet: madhan ravi am 21 Okt. 2018

0 Stimmen

EDIT 2
a = randi([0 9],84,7) %fake data to test
a1=a(:,2) %column you want to extract
a2=a(:,5) %column you want to extract
a=[a1 a2] %two columns stored in a matrix , sorry I missed this line which caused the error
idx = find(a1<a2)
a(idx,:)=fliplr(a(idx,:))
b = a(:,1) - a(:,2) % as column vector
b1 = b' %row vector

12 Kommentare

madhan ravi
madhan ravi am 21 Okt. 2018
if it doesn't meet your requirement let know
Young Lee
Young Lee am 21 Okt. 2018
Sorry It doesnt work,
madhan ravi
madhan ravi am 21 Okt. 2018
Bearbeitet: madhan ravi am 21 Okt. 2018
See edited answer now , no need loop to attain the desired result
Young Lee
Young Lee am 21 Okt. 2018
nope ,
madhan ravi
madhan ravi am 21 Okt. 2018
It’s like you gave in the example right? Am I missing something?
Young Lee
Young Lee am 21 Okt. 2018
Bearbeitet: Young Lee am 21 Okt. 2018
here's code I have used , I figured out all the odd rows were wrong in value as well
x = myreesult2 %EDITED a1= x(:,2); a2= x(:,5); idx = find(a1<a2); %its column 2 & 5 out of 84x7 matrice x(idx,:)=fliplr(x(idx,:)); b = x(:,2) - x(:,5) % as column vector
madhan ravi
madhan ravi am 21 Okt. 2018
Bearbeitet: madhan ravi am 21 Okt. 2018
use abs(the_value) to get positive result, if you don’t want to use abs() upload the matrix which you are testing because as you can see it works very well for the example you gave here
madhan ravi
madhan ravi am 21 Okt. 2018
Bearbeitet: madhan ravi am 21 Okt. 2018
Can you upload x(:,2) & x(:,5) datas here so that it can be tested
Young Lee
Young Lee am 21 Okt. 2018
sure, have look on a1 & a2
madhan ravi
madhan ravi am 21 Okt. 2018
check the edited code now
Young Lee
Young Lee am 21 Okt. 2018
thanks for your patience and help :)
madhan ravi
madhan ravi am 21 Okt. 2018
Bearbeitet: madhan ravi am 21 Okt. 2018
Anytime:)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by