storing values in matrix using for loop

4 views (last 30 days)
Hi I want to store values in a [3x3] matrix but getting error. Any help would be appreciated
for i=1:3
answerA(i,1)=1*i
answerB(i,2)=2*i
answerC(i,3)=i
% tableA=[answerA answerB answer C]
end
table=[reshape(answerA,[],1) reshape(answerB,[],1) reshape(answerC,[],1)]
% table=[reshape(answerA,[],1)]
ERROR:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in AllCurvesfit (line 80)
table=[reshape(answerA,[],1) reshape(answerB,[],1)]

Accepted Answer

KSSV
KSSV on 13 Mar 2019
Edited: KSSV on 13 Mar 2019
answerA = zeros(3,1) ;
answerB = zeros(3,1) ;
answerC = zeros(3,1) ;
for i=1:3
answerA(i)=1*i ;
answerB(i)=2*i ;
answerC(i)=i ;
end
table=[answerA answerB answerC]
May be you wanted:
T = table(answerA, answerB, answerC)
The above can be achieved without loops also:
i = (1:3)' ;
A = [i 2*i i]

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!