I want to make a matrix for each change of value

1 view (last 30 days)
I have a matrix S=ones(1x345) and I want to change the first value only of S, which is S(1,1) = 2 then save that matrix seperately lets say as S1(which has all values as 1 except S(1,1)). And after that, change the value only of S(1,2) = 2 and also save that matrix as lets say S2. And so on. Till I have S345 which has all values as 1 except S(1,345) as 2. So, what kind of code can be used. As I used a loop but it did not work out.
Thanks in advance

Accepted Answer

Walter Roberson
Walter Roberson on 5 Dec 2021
tfile = tempdir + "assign_S.m";
fid = fopen(tfile, 'w');
for K = 1 : 345
fprintf(fid, 'S%d = S; S%d(1,%d) = 2;\n', K, K, K);
end
fclose(fid);
S = ones(1,345);
run(tfile)
whos
Name Size Bytes Class Attributes K 1x1 8 double S 1x345 2760 double S1 1x345 2760 double S10 1x345 2760 double S100 1x345 2760 double S101 1x345 2760 double S102 1x345 2760 double S103 1x345 2760 double S104 1x345 2760 double S105 1x345 2760 double S106 1x345 2760 double S107 1x345 2760 double S108 1x345 2760 double S109 1x345 2760 double S11 1x345 2760 double S110 1x345 2760 double S111 1x345 2760 double S112 1x345 2760 double S113 1x345 2760 double S114 1x345 2760 double S115 1x345 2760 double S116 1x345 2760 double S117 1x345 2760 double S118 1x345 2760 double S119 1x345 2760 double S12 1x345 2760 double S120 1x345 2760 double S121 1x345 2760 double S122 1x345 2760 double S123 1x345 2760 double S124 1x345 2760 double S125 1x345 2760 double S126 1x345 2760 double S127 1x345 2760 double S128 1x345 2760 double S129 1x345 2760 double S13 1x345 2760 double S130 1x345 2760 double S131 1x345 2760 double S132 1x345 2760 double S133 1x345 2760 double S134 1x345 2760 double S135 1x345 2760 double S136 1x345 2760 double S137 1x345 2760 double S138 1x345 2760 double S139 1x345 2760 double S14 1x345 2760 double S140 1x345 2760 double S141 1x345 2760 double S142 1x345 2760 double S143 1x345 2760 double S144 1x345 2760 double S145 1x345 2760 double S146 1x345 2760 double S147 1x345 2760 double S148 1x345 2760 double S149 1x345 2760 double S15 1x345 2760 double S150 1x345 2760 double S151 1x345 2760 double S152 1x345 2760 double S153 1x345 2760 double S154 1x345 2760 double S155 1x345 2760 double S156 1x345 2760 double S157 1x345 2760 double S158 1x345 2760 double S159 1x345 2760 double S16 1x345 2760 double S160 1x345 2760 double S161 1x345 2760 double S162 1x345 2760 double S163 1x345 2760 double S164 1x345 2760 double S165 1x345 2760 double S166 1x345 2760 double S167 1x345 2760 double S168 1x345 2760 double S169 1x345 2760 double S17 1x345 2760 double S170 1x345 2760 double S171 1x345 2760 double S172 1x345 2760 double S173 1x345 2760 double S174 1x345 2760 double S175 1x345 2760 double S176 1x345 2760 double S177 1x345 2760 double S178 1x345 2760 double S179 1x345 2760 double S18 1x345 2760 double S180 1x345 2760 double S181 1x345 2760 double S182 1x345 2760 double S183 1x345 2760 double S184 1x345 2760 double S185 1x345 2760 double S186 1x345 2760 double S187 1x345 2760 double S188 1x345 2760 double S189 1x345 2760 double S19 1x345 2760 double S190 1x345 2760 double S191 1x345 2760 double S192 1x345 2760 double S193 1x345 2760 double S194 1x345 2760 double S195 1x345 2760 double S196 1x345 2760 double S197 1x345 2760 double S198 1x345 2760 double S199 1x345 2760 double S2 1x345 2760 double S20 1x345 2760 double S200 1x345 2760 double S201 1x345 2760 double S202 1x345 2760 double S203 1x345 2760 double S204 1x345 2760 double S205 1x345 2760 double S206 1x345 2760 double S207 1x345 2760 double S208 1x345 2760 double S209 1x345 2760 double S21 1x345 2760 double S210 1x345 2760 double S211 1x345 2760 double S212 1x345 2760 double S213 1x345 2760 double S214 1x345 2760 double S215 1x345 2760 double S216 1x345 2760 double S217 1x345 2760 double S218 1x345 2760 double S219 1x345 2760 double S22 1x345 2760 double S220 1x345 2760 double S221 1x345 2760 double S222 1x345 2760 double S223 1x345 2760 double S224 1x345 2760 double S225 1x345 2760 double S226 1x345 2760 double S227 1x345 2760 double S228 1x345 2760 double S229 1x345 2760 double S23 1x345 2760 double S230 1x345 2760 double S231 1x345 2760 double S232 1x345 2760 double S233 1x345 2760 double S234 1x345 2760 double S235 1x345 2760 double S236 1x345 2760 double S237 1x345 2760 double S238 1x345 2760 double S239 1x345 2760 double S24 1x345 2760 double S240 1x345 2760 double S241 1x345 2760 double S242 1x345 2760 double S243 1x345 2760 double S244 1x345 2760 double S245 1x345 2760 double S246 1x345 2760 double S247 1x345 2760 double S248 1x345 2760 double S249 1x345 2760 double S25 1x345 2760 double S250 1x345 2760 double S251 1x345 2760 double S252 1x345 2760 double S253 1x345 2760 double S254 1x345 2760 double S255 1x345 2760 double S256 1x345 2760 double S257 1x345 2760 double S258 1x345 2760 double S259 1x345 2760 double S26 1x345 2760 double S260 1x345 2760 double S261 1x345 2760 double S262 1x345 2760 double S263 1x345 2760 double S264 1x345 2760 double S265 1x345 2760 double S266 1x345 2760 double S267 1x345 2760 double S268 1x345 2760 double S269 1x345 2760 double S27 1x345 2760 double S270 1x345 2760 double S271 1x345 2760 double S272 1x345 2760 double S273 1x345 2760 double S274 1x345 2760 double S275 1x345 2760 double S276 1x345 2760 double S277 1x345 2760 double S278 1x345 2760 double S279 1x345 2760 double S28 1x345 2760 double S280 1x345 2760 double S281 1x345 2760 double S282 1x345 2760 double S283 1x345 2760 double S284 1x345 2760 double S285 1x345 2760 double S286 1x345 2760 double S287 1x345 2760 double S288 1x345 2760 double S289 1x345 2760 double S29 1x345 2760 double S290 1x345 2760 double S291 1x345 2760 double S292 1x345 2760 double S293 1x345 2760 double S294 1x345 2760 double S295 1x345 2760 double S296 1x345 2760 double S297 1x345 2760 double S298 1x345 2760 double S299 1x345 2760 double S3 1x345 2760 double S30 1x345 2760 double S300 1x345 2760 double S301 1x345 2760 double S302 1x345 2760 double S303 1x345 2760 double S304 1x345 2760 double S305 1x345 2760 double S306 1x345 2760 double S307 1x345 2760 double S308 1x345 2760 double S309 1x345 2760 double S31 1x345 2760 double S310 1x345 2760 double S311 1x345 2760 double S312 1x345 2760 double S313 1x345 2760 double S314 1x345 2760 double S315 1x345 2760 double S316 1x345 2760 double S317 1x345 2760 double S318 1x345 2760 double S319 1x345 2760 double S32 1x345 2760 double S320 1x345 2760 double S321 1x345 2760 double S322 1x345 2760 double S323 1x345 2760 double S324 1x345 2760 double S325 1x345 2760 double S326 1x345 2760 double S327 1x345 2760 double S328 1x345 2760 double S329 1x345 2760 double S33 1x345 2760 double S330 1x345 2760 double S331 1x345 2760 double S332 1x345 2760 double S333 1x345 2760 double S334 1x345 2760 double S335 1x345 2760 double S336 1x345 2760 double S337 1x345 2760 double S338 1x345 2760 double S339 1x345 2760 double S34 1x345 2760 double S340 1x345 2760 double S341 1x345 2760 double S342 1x345 2760 double S343 1x345 2760 double S344 1x345 2760 double S345 1x345 2760 double S35 1x345 2760 double S36 1x345 2760 double S37 1x345 2760 double S38 1x345 2760 double S39 1x345 2760 double S4 1x345 2760 double S40 1x345 2760 double S41 1x345 2760 double S42 1x345 2760 double S43 1x345 2760 double S44 1x345 2760 double S45 1x345 2760 double S46 1x345 2760 double S47 1x345 2760 double S48 1x345 2760 double S49 1x345 2760 double S5 1x345 2760 double S50 1x345 2760 double S51 1x345 2760 double S52 1x345 2760 double S53 1x345 2760 double S54 1x345 2760 double S55 1x345 2760 double S56 1x345 2760 double S57 1x345 2760 double S58 1x345 2760 double S59 1x345 2760 double S6 1x345 2760 double S60 1x345 2760 double S61 1x345 2760 double S62 1x345 2760 double S63 1x345 2760 double S64 1x345 2760 double S65 1x345 2760 double S66 1x345 2760 double S67 1x345 2760 double S68 1x345 2760 double S69 1x345 2760 double S7 1x345 2760 double S70 1x345 2760 double S71 1x345 2760 double S72 1x345 2760 double S73 1x345 2760 double S74 1x345 2760 double S75 1x345 2760 double S76 1x345 2760 double S77 1x345 2760 double S78 1x345 2760 double S79 1x345 2760 double S8 1x345 2760 double S80 1x345 2760 double S81 1x345 2760 double S82 1x345 2760 double S83 1x345 2760 double S84 1x345 2760 double S85 1x345 2760 double S86 1x345 2760 double S87 1x345 2760 double S88 1x345 2760 double S89 1x345 2760 double S9 1x345 2760 double S90 1x345 2760 double S91 1x345 2760 double S92 1x345 2760 double S93 1x345 2760 double S94 1x345 2760 double S95 1x345 2760 double S96 1x345 2760 double S97 1x345 2760 double S98 1x345 2760 double S99 1x345 2760 double ans 1x1 8 double fid 1x1 8 double tfile 1x1 166 string
  3 Comments
Walter Roberson
Walter Roberson on 5 Dec 2021
"can I use these matrices in the following way. To perform certain operation on each matrix?"
NO, you cannot.
"or how should I do it?"
Is the question how can you do it? Or is the question how should you do it? Those are extremely different questions. The answer to the question of how should you do it, is that you should never create such matrices at all. http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!