필터 지우기
필터 지우기

Easy Noob Indexing Question

조회 수: 4 (최근 30일)
Richard Keniuk
Richard Keniuk 2020년 12월 19일
댓글: Richard Keniuk 2020년 12월 27일
I'm sure this answer is looming in front of me but I am missing it somehow. I have an array
A = [0 0;1 -1; -1 1;1 -1; -1 1];
and
B = [2 -2; -2 2;2 -2; -2 2;2 -2; -2 2;2 -2;0 0];
and I'm concatenating these two in the vertical direction but I only want rows 4:7 of my concatenated output.
I am doing this in two steps.
C = cat(1,A,B); % or some other calculation of vectors A and B
C = C(4:7,:);
How can I reduce this to one command by directly indexing the equation of "cat(1,A,B)" directly into C?
I know that I could do this by C = cat(1,A(end-1:end,:),B(1:2,:)). A and B are of variable length and I only want C to hold the concatenation +/-2 indices vertically. Is there another way in a single line?
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 12월 19일
Is there any issue with that, simplest and easiest?
C=[A;B];
C=C(4:7,:)
Image Analyst
Image Analyst 2020년 12월 19일
My programs have thousands of lines of code. Why does yours have to be just one line of code instead of 2? Your last way of using end will work for variable sizes arrays.

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

채택된 답변

Rishabh Mishra
Rishabh Mishra 2020년 12월 23일
Hi,
Use the code below for resolving your issue:
C=[A;B];
C=C(4:7,:);
This is the simplest way to code the solution for the issue you are facing.
Hope this helps !
  댓글 수: 1
Richard Keniuk
Richard Keniuk 2020년 12월 27일
I guess I already had this to the "cleanest" implementation. I thought maybe I was missing something here.
Thanks for your help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by