필터 지우기
필터 지우기

how to specify numbers in an array with varying steps

조회 수: 4 (최근 30일)
A Poyser
A Poyser 2024년 6월 26일
답변: Star Strider 2024년 6월 26일
I want to specify the a group of numbers in an array but they do not appear in even steps.
I want to capture
N1 (1 and 12), N2 (2,3,4,10,11), N3(5,6,7,8,9) and N4 (13, 14)
I understand that to capture N1,and N4 I can specify the individual array numbers, and for N3 I can specify the start and the end and the size of the steps [5:1:9]. I just can't work out how to specify what I would like in the instance of N2
Please help.
Thanks
Alex
  댓글 수: 1
Aditya
Aditya 2024년 6월 26일
For N2 you can create it like this: N2 = [2:1:4, 10:1:11]

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

채택된 답변

Star Strider
Star Strider 2024년 6월 26일
For ‘N2’ and others, just use the individual aray indices in a vector —
N = rand(1,14)
N = 1x14
0.9276 0.2641 0.4683 0.1720 0.9337 0.3857 0.6798 0.7333 0.7803 0.1052 0.2883 0.7299 0.7880 0.4599
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
N2 = N([2:4 10:11])
N2 = 1x5
0.2641 0.4683 0.1720 0.1052 0.2883
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
N2 = N([2,3,4,10,11])
N2 = 1x5
0.2641 0.4683 0.1720 0.1052 0.2883
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Note the use of square brackets [] here to concatenate the individual elements.
.

추가 답변 (1개)

Namnendra
Namnendra 2024년 6월 26일
Hello Alex,
To specify a group of numbers in an array that do not appear in even steps, you can manually create the array by specifying each element individually. For `N2`, which includes the numbers `[2, 3, 4, 10, 11]`, you can simply list these numbers in an array.
Here's how you can define all the groups in MATLAB:
% Define the groups
N1 = [1, 12];
N2 = [2, 3, 4, 10, 11];
N3 = [5:1:9]; % or simply [5, 6, 7, 8, 9]
N4 = [13, 14];
% Display the groups
disp('N1:');
disp(N1);
disp('N2:');
disp(N2);
disp('N3:');
disp(N3);
disp('N4:');
disp(N4);
In the case of `N2`, you need to manually list each number because they do not follow a consistent step pattern. The other groups (`N1`, `N3`, and `N4`) can be defined using ranges or individual elements as appropriate.
Explanation:
- N1: `[1, 12]` - This group contains the numbers 1 and 12.
- N2: `[2, 3, 4, 10, 11]` - This group contains the numbers 2, 3, 4, 10, and 11. Since these numbers do not follow a consistent step pattern, you list them individually.
- N3: `[5:1:9]` - This group contains the numbers from 5 to 9 with a step size of 1. You can also specify it as `[5, 6, 7, 8, 9]`.
- N4: `[13, 14]` - This group contains the numbers 13 and 14.
By defining the groups in this way, you can easily capture and work with the specified numbers in your MATLAB code.
I hope the above information helps you.
Thank you.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by