Storing multiple values in one location of matrix

조회 수: 1 (최근 30일)
Aman
Aman 2011년 2월 3일
댓글: Hadeel 2022년 11월 8일
I have used 2 variables i and j . now i want to place their values in one place in matrix,like if i=1 and j=2 i want to store 12 at matrix location (1,1)
kindly tell me the way to do it.,. thank you

채택된 답변

Andreas Goser
Andreas Goser 2011년 2월 3일
I need to speculate a bit what the question is. Here an example code:
a=1; % I peronally never use i,j,I,J
b=2;
M=zeros(4);
M(3,3)=a; % Apply the content of a to position (3,3)
M(4,4)=b
Or is it something like that?
a=1; % I peronally never use i,j,I,J
b=2;
M=zeros(4);
M(3,3)=str2num([num2str(a), num2str(b)])
  댓글 수: 2
Aakash Bhatia
Aakash Bhatia 2017년 12월 3일
편집: Aakash Bhatia 2017년 12월 3일
Hi , thank you for working this out. But is there a way to seperate the two elements i.e. a and b in your case with a comma?
Thanks
Hadeel
Hadeel 2022년 11월 8일
please,sir could you to find a way for saperating beacause i need it please🙏

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

추가 답변 (2개)

Davide Ferraro
Davide Ferraro 2011년 2월 3일
If your need is to place the single digits to create a bigger number if you would like to avoid passing through string conversion (as suggested by Andreas) you can use sums and multiplications to create the number.
a=1;
b=2;
M=zeros(4);
M(1,1) = [a b] * [10 1]';
% For more elements
a = 1;
b = 2;
c = 3;
d = 4;
M(1,1) = [a b c d] * (10.^(3:-1:0))';
This means a * 10 + b * 1 = 12. In the second example I'm extending the notation to allow the definition of more terms without writing a * 1000 + b * 100 * c * 10 * d * 1.
If your need is to store two values (a matrix) in another matrix you need to use Cell Arrays:
C = {[a,b]}

Aman
Aman 2011년 2월 3일
Thank you so much for your support people..thank you so much:)

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by