Splitting of a column to multiple columns

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

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 ?
I want to replace the numbers I eliminate with 1 then split the column to multiple columns.
Thanks
jahanzaib ahmad
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
Stephen23
Stephen23 il 9 Lug 2019
Modificato: Stephen23 il 9 Lug 2019
Simpler:
n = max(1,n)
or
n(n<1) = 1
sir @Stephen Cobeldick whats the easy way to split 100x1 into 10 columns of 10x1
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,:);
Stephen23
Stephen23 il 10 Lug 2019
Modificato: Stephen23 il 10 Lug 2019
" 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).

Accedi per commentare.

Risposte (1)

Renato SL
Renato SL il 9 Lug 2019
After doing what Stephen Cobeldick suggested in the comments, I believe this would do it.
n2 = reshape(n,[],10);

6 Commenti

Thank you.
I would like to ask an additional question.
for example I have a column of 1000 by 1 matrix and I want to split it into 10 columns each, and each column should have 10 rows. So that after the 10th column and 10th row for the first round of splits, the split would start again from the 1st row and add the next 10 rows.
To clarify: 10th column would have 10 rows of 91 - 100, thereafter the next split would start from the 1st column with another 10 rows starting from 101 - 110 and so on till it gets to 1000
Thanks
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?
Yes that is correct, however I want to have 10 of (10 by 10) matrix till 1000, so each of the 10 columns will have 100 rows, but next split after the first split of 10 by 10 would start from the 1st column with another 10 rows starting from 101 - 110 and so on till it gets to 1000.
I think a loop would be needed such that 1st column will be (1 -10) and 10th column ( 91 -100), then start from 1st column with the next 10 rows of (101 - 110), 2nd column with next 10 rows of ( 111 - 120) ...... and 10th column with 10 rows of (191 - 200) and so on.
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
Thanks for your help. This works
Stephen23
Stephen23 il 12 Lug 2019
Modificato: Stephen23 il 12 Lug 2019
@Olu B: note that splitting up your data makes it harder to work with.
Using indexing is much simpler.

Accedi per commentare.

Categorie

Richiesto:

il 9 Lug 2019

Modificato:

il 12 Lug 2019

Community Treasure Hunt

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

Start Hunting!

Translated by