Why the matrix is changing from 10X10 to 1X100

2 ビュー (過去 30 日間)
Offroad Jeep
Offroad Jeep 2016 年 10 月 13 日
コメント済み: Offroad Jeep 2016 年 10 月 13 日
My question details in steps 1. generate 10X10 matrix, ones(10) 2. randomly select a site and change it to -1 3. repeat till all the 1 changes to -1 in the matrix. Thanks
clc
clear all
clearvars
format compact
nrows = 10
fm_layer = ones(nrows)
E_A = sum(sum(fm_layer))
n = numel(fm_layer);
for k=1:n
A(k) = -1
E_B = sum(sum(fm_layer))
end
  5 件のコメント
Guillaume
Guillaume 2016 年 10 月 13 日
If that's what you want, then I believe my answer does it.
Offroad Jeep
Offroad Jeep 2016 年 10 月 13 日
Thanks ..... I did it ....

サインインしてコメントする。

回答 (2 件)

Alexandra Harkai
Alexandra Harkai 2016 年 10 月 13 日
Seems like a new array A is created and gets expanded at every iteration. Since the 'numel' of your matrix is 100 (=10*10), it loops through and creates an array of length 100, expanding it element-by-element.

Guillaume
Guillaume 2016 年 10 月 13 日
If you want to replace the elements of fm_layer one by one in some random order then:
fm_layer = ones(nrows);
for idx = randperm(numel(fm_layer))
fm_layer(idx) = -1;
%do something with modifier fm_layer for this step
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by