Error creating matrix using a integer and vector in Matlab

I'm having trouble understanding why I can''t declare a matrix by using the following declaration A = [1 x x.^2 x.^3]; where x is from a column vector of 1x50 double

 채택된 답변

madhan ravi
madhan ravi 2020년 9월 15일
편집: madhan ravi 2020년 9월 15일
A = x(:) .^ (0:3)

댓글 수: 6

Sorry that was my mistake on posting the question. I updated it. the error im getting is Error using horzcat
Dimensions of arrays being concatenated are not consistent.
the 1 seems to be causing an issue since the rest are a 1x50 double but I don't want to have to create a seperate ones array just to put into my matrix
I suggest you to do MATLAB On-ramp course. You seem to have a problem understanding what a row and a column represent. It’s the first term to know when getting into MATLAB.
I would like to have it output as a 50x4 matrix with the first column being just 1s. the answer you posted results in a 1x151 double.
thank you, could you post that as your answer so i can accept it?

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

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 9월 16일
편집: Steven Lord 2020년 9월 16일
madhan ravi has told you how to do this but I want to explain why your original approach didn't work.
If x is a column vector with 50 elements it has 50 rows (if it were a 1x50 array it would be a row vector.) The number 1 has 1 row. You can't horizontally concatenate an array with one row and an array with 50 rows and have them form a rectangular array as you can see here:
bar([1 2], [1 50])
You could make that 1 an array with the same number of rows as x:
x = (1:10).';
A1 = [ones(size(x)), x, x.^2, x.^3];
Though I'd probably reverse the order of the columns to match the convention of functions like polyfit and polyval.
A2 = x.^(3:-1:0)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2020년 9월 15일

편집:

2020년 9월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by