creating a 8x8 matrix with only -1
Afficher commentaires plus anciens
i want to create a 8x8 matrix withn only the number -1 how do i do it?
Réponses (3)
Use "ones" to build a matrix of ones and multiply it by (-1).
1 commentaire
Walter Roberson
le 28 Oct 2024
You have to be careful to build up the 8 x 8 size for the ones() call, out of pure -1's. It is certainly possible to do.
-ones(8)
Append at 20241029 as a Cody challenge:
rand(single('('-' '))
%You don't even need -1 in your code. This can be applied to any challenge that has a limit on the number that appears in the code.
%If you're okay with using characters that aren't on your keyboard, you can even just use a Backspace character (U+0008).
6 commentaires
Walter Roberson
le 28 Oct 2024
This uses 8, which violates the rule that only -1 can be used.
This is a very willful interpretation of the task. My guess was it's simply to create a 8x8 matrix of -1's.
埃博拉酱
le 29 Oct 2024
Yes, any normal person would generate an 8×8 matrix of -1 all the way I wrote it. If you mean that the number that appears in the code can only have -1, I guess this is more appropriate as a Cody challenge rather than as a question in the Answers forum, which is more geared towards practical scenarios.
Excluding parentheses, arithmetic operators, and the output variable name, this uses nothing but negative ones (pun intended)
A = -ones((-1 + -1 + -1 + -1)*(-1 + -1))
DGM
le 29 Oct 2024
D'oh!
I must have had my blinders on when I was coming up with that.
Aimee
le 29 Oct 2024
0 votes
7 commentaires
Image Analyst
le 29 Oct 2024
Or you could simply do
A = [
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 ]
though that's not a very MATLABy way of doing it, but it does use only -1's and no 8's.
Walter Roberson
le 29 Oct 2024
Modifié(e) : Image Analyst
le 30 Oct 2024
This (Aimee's) solution involves the number 8, contrary to the requirement that the solution involves only -1
Walter Roberson
le 30 Oct 2024
The requirement is "creating a 8x8 matrix with only -1" . Using an 8 in the command does not fit the requirement.
Torsten
le 30 Oct 2024
I'm not a native speaker, but isn't the usual interpretation of "creating a 8x8 matrix with only -1":
create a 8x8 matrix which has only -1's as elements
?
Aimee
le 10 Déc 2024
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!