Splitting of a column to multiple columns
Mostra commenti meno recenti
Hi,
If I have a 100 x 1 column of numbers between -1 to 100 and i want to eliminate the 0's and negative numbers, then split the column into 10 rows each and call each row a given name so as to plot them against each other.
Could you advise how to do this, thanks
6 Commenti
jahanzaib ahmad
il 9 Lug 2019
when u will eliminate negative numbers or zeros with what number u want to replace those null values ? and if u just elimenate zeros or negative number e.g u are left with 95 number .how that can be equally split ?
Olu B
il 9 Lug 2019
jahanzaib ahmad
il 9 Lug 2019
Modificato: jahanzaib ahmad
il 9 Lug 2019
replace 0 or negative values with 1
n=(-1+(2-(-1)).*rand(100,1));
m=n;
for i=1:length(n)
if n(i)<0
n(i)=1;
end
end
Simpler:
n = max(1,n)
or
n(n<1) = 1
jahanzaib ahmad
il 9 Lug 2019
a=n(1:10,:);b=n(11:20,:);c=n(21:30,:);d=n(31:40,:);e=n(41:50,:);f=n(51:60,:);g=n(61:70,:);h=n(71:80,:);i=n(81:90,:);
j=n(91:100,:);
" whats the easy way to split 100x1 into 10 columns of 10x1 "
There is no easy way to do that "automatically". In fact, you should avoid doing that (unless you want to force yourself into writing slow, complex, buggy code which is hard to debug).
The best solutions would be to either reshape the array (e.g. as Renato shows), or split the data into a cell array (e.g. using num2cell or mat2cell).
Risposte (1)
Renato SL
il 9 Lug 2019
1 voto
After doing what Stephen Cobeldick suggested in the comments, I believe this would do it.
n2 = reshape(n,[],10);
6 Commenti
Olu B
il 10 Lug 2019
Renato SL
il 10 Lug 2019
From what I understand, this time you initially have one (1000 x 1) matrix and in the end, you want to have 10 of (10 x 10) matrix.
Is this correct?
Olu B
il 10 Lug 2019
Renato SL
il 10 Lug 2019
For that, I recommend using reshape function again. I think it would look like this.
Say you have matrix n (1000 by 1) originally, then
n = [1:1000]';
n2 = reshape(n,10,10,10);
so that n would be reshaped into a (10 by 10 by 10) matrix. The first 100 entries then would look like
n2(:,:,1)
ans =
1 11 21 31 41 51 61 71 81 91
2 12 22 32 42 52 62 72 82 92
3 13 23 33 43 53 63 73 83 93
4 14 24 34 44 54 64 74 84 94
5 15 25 35 45 55 65 75 85 95
6 16 26 36 46 56 66 76 86 96
7 17 27 37 47 57 67 77 87 97
8 18 28 38 48 58 68 78 88 98
9 19 29 39 49 59 69 79 89 99
10 20 30 40 50 60 70 80 90 100
The second 100
n2(:,:,2)
ans =
101 111 121 131 141 151 161 171 181 191
102 112 122 132 142 152 162 172 182 192
103 113 123 133 143 153 163 173 183 193
104 114 124 134 144 154 164 174 184 194
105 115 125 135 145 155 165 175 185 195
106 116 126 136 146 156 166 176 186 196
107 117 127 137 147 157 167 177 187 197
108 118 128 138 148 158 168 178 188 198
109 119 129 139 149 159 169 179 189 199
110 120 130 140 150 160 170 180 190 200
and so on.
Then to keep each 100 as separate matrices, simply do something like
n100 = n2(:,:,1);
n200 = n2(:,:,2);
%and so on
Olu B
il 11 Lug 2019
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!