I have a 23×4 matrix A and a 23×1 logical array B
A = [ 2619834.52 1169193.206 2622778.731 1168042.07 2622778.731 1168042.07 2623330.871 1167853.553 2623330.871 1167853.553 2623895.366 1167330.066 2623895.366 1167330.066 2623752.209 1167450.319 2623895.366 1167330.066 2623752.209 1167450.319 2623900.076 1167326.26 2624169.326 1167219.875 2624169.326 1167219.875 2624253.37 1167198.916 2624253.37 1167198.916 2624535.39 1167048.569 2624253.37 1167198.916 2624535.39 1167048.569 2625504.044 1167095.529 2626025.669 1167318.065 2625552.809 1167096.398 2625957.908 1167292.096 2625592.67 1167098.709 2625720.968 1167147.911 2625637.183 1167100.023 2625720.968 1167147.911 2625720.968 1167147.911 2625749.984 1167166.109 2625739.693 1167164.891 2625957.908 1167292.096 2625749.984 1167166.109 2625880.835 1167248.686 2625880.835 1167248.686 2625990.736 1167298.256 2625552.809 1167096.398 2625957.908 1167292.096 2625880.835 1167248.686 2625990.736 1167298.256 2626025.669 1167318.065 2625957.908 1167292.096 2628613.339 1167794.423 2628793.988 1167686.086 2628793.988 1167686.086 2629072.311 1167703.565 2629072.311 1167703.565 2631163.404 1169347.638]; B = [ 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0];
I need to create a 2-column matrix C, where each row of C is a row of A, but picking up only the elements of both the 1st and 2nd columns of A, when the corrisponding element of B = 0, and taking only the elements of both the 3rd and 4th columns of A, when the corresponding element of B = 1. My current "solution" is…
ticif B==0 C = A(:,1:2) else C = A(:,3:4)endtoc% Result
C = 2622778.731 1168042.07 2623330.871 1167853.553 2623895.366 1167330.066 2623752.209 1167450.319 2623752.209 1167450.319 2624169.326 1167219.875 2624253.37 1167198.916 2624535.39 1167048.569 2624535.39 1167048.569 2626025.669 1167318.065 2625957.908 1167292.096 2625720.968 1167147.911 2625720.968 1167147.911 2625749.984 1167166.109 2625957.908 1167292.096 2625880.835 1167248.686 2625990.736 1167298.256 2625957.908 1167292.096 2625990.736 1167298.256 2625957.908 1167292.096 2628793.988 1167686.086 2629072.311 1167703.565 2631163.404 1169347.638Elapsed time is 0.000728 seconds.
…I would like to avoid the "if", and therefore to use a more compact code, maybe only one line (and not slower than now, obviously). Any idea?
Best Answer