How do I create an array for the square of numbers from 1 to 100?
19 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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]
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!