Create a matrix using elements of other matrices in MATLAB
问题 I have two matrices a and b (with equal number of columns). I want to create a third matrix c using a condition: For example, I have: a = [1 2 3 4 1 2 3 4 1 2 3 4; 1 1 1 1 2 2 2 2 3 3 3 3] b = [5 6 7 8 9 10 11 12 13 14 15 16; 17 18 19 20 21 22 23 24 25 26 27 28; 29 30 31 32 33 34 35 36 37 38 39 40] The condition is: a(2, :) == 2 , so the resulting matrix should be: c = [1 2 3 4; 2 2 2 2; 9 10 11 12; 21 22 23 24; 33 34 35 36] 回答1: Try this %With your a and b cols = a(2,:) == 2; c = [a(:,cols)