Help with matrix . Error message is being displayed : Subscript indices must either be real positive integers or logicals. Error in implicit (line 26) A(i,0) = a;

조회 수: 1 (최근 30일)
function [] = implicit(~)
%first non-linear equation.
err=input('min_error');
n= input('number_of_iterations');
m= input('number_of_points');
%drichlet boundary conditions
a=input('right hand side temp =');
b=input('left hand side temp = ');
A = zeros(m+1,n+1);
for i=1:m+1
A(i,0) = a;
end
for i=1:n+1
A(i,n+1) = b;
end
disp(A);
end

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 22일
A = zeros(m+1,n+1);
A(:,1) = a;
A(:,end) = b;
  댓글 수: 3
Jan
Jan 2017년 7월 22일
@Anshuman S: It is typical for Matlab that you can replace for loops by "vectorizing": You can move the indexing from the for command directly into the assignment:
for i=1:m+1
A(i,1) = a;
end
Take the "1:m+1" from for and move it to:
A(1:m+1,1) = a;
If A is pre-allocated as in Walter's answer, the index can be even omitted and the ":" is used:
A(:, 1) = a;
This is nicer, leaner and faster than the loop. While the speed might not matter now, leaner code is a benefit, because you have less chances for typos! :-)

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

추가 답변 (1개)

John BG
John BG 2017년 7월 20일
MATLAB matrix indexing does not use null or negative indices.
You can define reference vectors as function of a given matrix that take all sorts of values value, positive, negative or null values.
But once the reference value found, the index and not the reference value is the index to apply to the matrix being read.
When retrieving matrix elements through indexing, you have to pass the matrix index that corresponds to the sought elements, and such indices have to be positive and not null.
John BG
  댓글 수: 2
Anshuman S
Anshuman S 2017년 7월 20일
Thanks John; Could you please tell me; what are the changes I have to make in my code ? ( I want to make a m+1Xn+1 matrix and want to set the first and the last col of this matrix as a & b , which are user defined input number.
John BG
John BG 2017년 7월 23일
ok, my understanding of your question is you asking for the following:
clear all;close all
a=input('right hand side temp =');
b=input('left hand side temp = ');
n= input('number_of_iterations');
m= input('number_of_points');
A(1)=1;A(end)=b;
or perhaps you mean this
A([1:1:m])=a;
A([m+1:1:n+1])=b;
I doubt there's need for any for loop at all for the kind of matrix assignment you are asking for
John BG

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

카테고리

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