Adding data to two matrices for indexing

조회 수: 1 (최근 30일)
Joseph Lee
Joseph Lee 2017년 11월 8일
답변: Stephen23 2017년 11월 8일
I have 2 matrices Z and Y, both are a 10x1300 matrix and consists of random numbers, how do i add data to the left and right of each element and then use indexing to find Y for a certain value of Z
For example,
Z [10 1300]
Y [10 1300]
One element in Z:
Z(1,1)=10
z=Z(1,1)
Add data to left and right side,
z-3,z-2,z-1,z,z+1,z+2,z+3
Final result: 7,8,9,10,11,12,13
Similar for Y but with different data additions
One element in Y:
Y(1,1)=20
y=Y(1,1)
Add data to left and right side,
y+3,y+2,y+1,y,y+1,y+2,y+3
Final result: 23,22,21,20,21,22,23
Use indexing to find Z=7 then find the respective element from Y would give Y= 23,
Y(find(Z==7))=23
Z=8,Y=22
Z=9,Y=21
Z=10,Y=20
Z=11,Y=21
Z=12,Y=22
Z=13,Z=23
  댓글 수: 2
Guillaume
Guillaume 2017년 11월 8일
편집: Guillaume 2017년 11월 8일
I have no idea what it is you're asking. Your explanation is really unclear. Let's start with some smaller Y and Z, e.g. given:
Z = [10 7 9
15 1 23];
and
Y = [20 1 13
5 7 9];
what exact result do you want (and how do you get at it)?
result = [? ? ?
? ? ?];
Is it even a matrix the same size as Y and Z that you want as a result?
Joseph Lee
Joseph Lee 2017년 11월 8일
편집: Joseph Lee 2017년 11월 8일
final Z
[7 8 9 10 11 12 13 ¦ 4 5 6 7 8 9 10 ¦ 6 7 8 9 10 11 12
12 13 14 15 16 17 ¦-2 1 0 1 2 3 4 ¦ 20 21 22 23 24 25 26]
Final Y= [23 22 21 20 21 22 23¦ 4 3 2 1 2 3 4 ¦ 16 15 14 13 14 15 16
8 7 6 5 6 7 8¦ 10 9 8 7 8 9 10 ¦ 12 11 10 9 10 11 12]
both are same sized, i need a way to add additional 6 elements into each and every element in the matrix
you can remove the ¦

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

채택된 답변

Stephen23
Stephen23 2017년 11월 8일
>> Z = [1,9;6,1];
>> cell2mat(arrayfun(@(m)m-3:+1:m+3,Z,'uni',0))
ans =
-2 -1 0 1 2 3 4 6 7 8 9 10 11 12
3 4 5 6 7 8 9 -2 -1 0 1 2 3 4
>> Y = [9,1;4,9];
>> cell2mat(arrayfun(@(m)[m+3:-1:m,m+1:m+3],Y,'uni',0))
ans =
12 11 10 9 10 11 12 4 3 2 1 2 3 4
7 6 5 4 5 6 7 12 11 10 9 10 11 12
>>

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by