I have a 16 x 16 matrix defined and I need to pick the elements from this matrix based on some other numeric data that I will be processing. Eg: if the data is 26, I need to pick the element in row 2 column 6. Can someone please suggest a code for this?
Thanks in advance.

 채택된 답변

Star Strider
Star Strider 2017년 1월 30일
편집: Star Strider 2017년 1월 30일

1 개 추천

My approach:
A = randi(99, 16); % Create Matrix
data = 26;
row = fix(data/10)
col = rem(data,10)
Desired_Element = A(row,col)
EDIT I have no idea how you would format references to elements (5,10), (10,5) or (16,16) since you did not give an example of those. My code here works for the problem you posted (single-digit subscripts), but would have to be revised to generalise to those other conditions, depending on how you format your ‘data’ reference.

댓글 수: 5

fiona rozario
fiona rozario 2017년 1월 31일
Thanks a lot for the help...
My data values would have a maximum value of 256. So in case of 256, I will refer to the element in row 2, column 6. I get the column value from the method given above. How do I get the row value?
Thanks in advance...
Star Strider
Star Strider 2017년 1월 31일
My pleasure.
Your (16x16) matrix would have 256 elements. Do you want the maximum value in your matrix, or element 256?
If you want to directly get element 256, Honglei Chen’s ind2sub approach is best.
If you want to choose the maximum values in your matrix, then Honglei Chen’s ind2sub approach or the find function with row and column outputs is likely to be the most efficient. These would also use the max function.
An example would help us determine the most appropriate way to solve your problem.
fiona rozario
fiona rozario 2017년 2월 1일
I will be referring to the elements of an S box (attached image) as per the data I have. The data is actually pixel values of an image which I am trying to encrypt. So, if the pixel value is 256 (which is the maximum in each component of an RGB image), I want to refer to row 2 and column 6 and substitute 'F7' in place of '256'. Similarly, a pixel value of '99' should look up row 9 and column 9 and substitute 'EE' in place of '99'.
Using the first and last digits of the pixel value to map to row number and column number is just my design (something similar to what is done in AES). Any suggestions on a better way to look up the S box matrix is most welcome.
Star Strider
Star Strider 2017년 2월 1일
I don’t have any experience with encryption. I may not be able to provide you further help.
How would you generalise your table lookup to pixels with a value of 206, 236, or something similar? Obviously, considering only the first and last digits in a pixel value would not provide a unique map to your table in this instance.
fiona rozario
fiona rozario 2017년 2월 1일
Thanks for pointing that out...I hadn't thought of that.

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

추가 답변 (1개)

Honglei Chen
Honglei Chen 2017년 1월 30일

1 개 추천

You may want to look at ind2sub function, but do you mean data = 22? I couldn't see how the 2nd row 6th column maps to element 26.
HTH

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 1월 30일

댓글:

2017년 2월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by