How do I create an array for the square of numbers from 1 to 100?

조회 수: 2 (최근 30일)
Brooke
Brooke 2023년 9월 22일
답변: Dyuman Joshi 2023년 9월 22일
This is my code so far:
%Assignment 1 code
%Problem 1
f = f;
a1 = ones(100,1);
a2 = [1:1:100];
a2 = transpose(a2);
A = cat(2,a2,a1);
k = A./f;
%Problem 2
a3 = ????
But i need to make an array that does the following:
Has the first column representing the numbers of 1 to 100 but each is squared (1,4,9,16...10,000), the second is just 1 to 100, and the third is just ones the whole way down.
I already have the second two (Stored in a1 and a1) but I'm not sure how I can create an array that has those squared values outside of typing them all in and I REALLY don't want to do that. Pls help.

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 9월 22일
a1 = ones(100,1);
%Avoid using the superfluous [], as they are used for concatenation
a2 = (1:100)';
A = [a2.^2 a2 a1]
A = 100×3
1 1 1 4 2 1 9 3 1 16 4 1 25 5 1 36 6 1 49 7 1 64 8 1 81 9 1 100 10 1

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by